当前位置:网站首页>Double Strings (别总忘记substr)
Double Strings (别总忘记substr)
2022-08-02 03:08:00 【lovesickman】
Double Strings (别总忘记substr)
语法
substr(size_type _Off = 0,size_type _Count = npos) // 开始位置,长度(默认是到结尾)
一种构造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中从pos开始的len个字符的拷贝(pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
异常 :若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
题目描述
You are given $ n $ strings $ s_1, s_2, \dots, s_n $ of length at most $ \mathbf{8} $ .
For each string $ s_i $ , determine if there exist two strings $ s_j $ and $ s_k $ such that $ s_i = s_j + s_k $ . That is, $ s_i $ is the concatenation of $ s_j $ and $ s_k $ . Note that $ j $ can be equal to $ k $ .
Recall that the concatenation of strings $ s $ and $ t $ is $ s + t = s_1 s_2 \dots s_p t_1 t_2 \dots t_q $ , where $ p $ and $ q $ are the lengths of strings $ s $ and $ t $ respectively. For example, concatenation of “code” and “forces” is “codeforces”.
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases.
The first line of each test case contains a single integer $ n $ ( $ 1 \leq n \leq 10^5 $ ) — the number of strings.
Then $ n $ lines follow, the $ i $ -th of which contains non-empty string $ s_i $ of length at most $ \mathbf{8} $ , consisting of lowercase English letters. Among the given $ n $ strings, there may be equal (duplicates).
The sum of $ n $ over all test cases doesn’t exceed $ 10^5 $ .
输出格式
For each test case, output a binary string of length $ n $ . The $ i $ -th bit should be $ \texttt{1} $ if there exist two strings $ s_j $ and $ s_k $ where $ s_i = s_j + s_k $ , and $ \texttt{0} $ otherwise. Note that $ j $ can be equal to $ k $ .
样例 #1
样例输入 #1
3
5
abab
ab
abc
abacb
c
3
x
xx
xxx
8
codeforc
es
codes
cod
forc
forces
e
code
样例输出 #1
10100
011
10100101
提示
In the first test case, we have the following:
- $ s_1 = s_2 + s_2 $ , since $ \texttt{abab} = \texttt{ab} + \texttt{ab} $ . Remember that $ j $ can be equal to $ k $ .
- $ s_2 $ is not the concatenation of any two strings in the list.
- $ s_3 = s_2 + s_5 $ , since $ \texttt{abc} = \texttt{ab} + \texttt{c} $ .
- $ s_4 $ is not the concatenation of any two strings in the list.
- $ s_5 $ is not the concatenation of any two strings in the list.
Since only $ s_1 $ and $ s_3 $ satisfy the conditions, only the first and third bits in the answer should be $ \texttt{1} $ , so the answer is $ \texttt{10100} $ .
写的时候,用set写了非常丑陋的代码。不过set是真的好用。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){
for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){
j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){
for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){
for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {
if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+10,M=1e9+7;
ll n,m,_;
string s[N];
bool ans[N];
set<string>st;
bool ck(string s,string t){
// s删除t
int pos = s.find(t);
if(pos==-1)return false;
string tmp = "";
for(int i=pos;i<s.size();i++){
tmp+=s[i];
}
if(st.find(tmp) == st.end())return -1;
return 1;
}
void solve(){
cin>>n;
st.clear();
fo(i,1,n){
cin>>s[i];
st.insert(s[i]);
}
for(int i=1;i<=n;i++){
string t = s[i]; // 8
st.erase(s[i]); // 8
string tmp = "";
for(int j=0;j<t.size();j++){
tmp += t[j];
if(ck(t,tmp)){
ans[i]=1;
}
}
st.insert(s[i]);
}
fo(i,1,n){
cout<<ans[i];
}
cout<<endl;
}
int main(){
cin>>_;
while(_--){
solve();
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200007;
const int MOD = 1000000007;
void solve() {
int n;
cin >> n;
string s[n];
map<string, bool> mp;
for (int i = 0; i < n; i++) {
cin >> s[i];
mp[s[i]] = true;
}
for (int i = 0; i < n; i++) {
bool ok = false;
for (int j = 1; j < s[i].length(); j++) {
string pref = s[i].substr(0, j), suff = s[i].substr(j, s[i].length() - j);
if (mp[pref] && mp[suff]) {
ok = true;}
}
cout << ok;
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt; cin >> tt; for (int i = 1; i <= tt; i++) {
solve();}
// solve();
}
边栏推荐
猜你喜欢

Chapter 11_Database Design Specifications

mysql8.0.28 download and installation detailed tutorial, suitable for win11

WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用

面试必备!TCP协议经典十五连问!

PHP WebSehll backdoor script and detection tool

常见的SQL面试题:经典50例

22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志

JunitTest单元测试

mysql8.0.28下载和安装详细教程,适配win11

Tree Chain Segmentation-
随机推荐
PHP WebSehll 后门脚本与检测工具
Heao Technology Network Interview (with reference answers)
三维数字孪生引擎与实景互动,案例解析
MySQL8--Windows下使用压缩包安装的方法
请教各位大佬,如果我代码里面设置了,这个id我在什么地方可以查到呢?连接到mysql cluste
Webshell upload method
[LeetCode] 83. Delete duplicate elements in the sorted list
MySQL函数(经典收藏)
MySQL中的存储过程(详细篇)
22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志
PyTorch(六)——PyTorch可视化
OperatingSystemMXBean to get system performance metrics
基于优化的多核局部费舍尔判别分析的故障分类
生成器知道鉴别器在无条件GANs中应该学习什么
【LeetCode】145. Postorder Traversal of Binary Tree
How ReentrantLock works
【LeetCode】102. Level order traversal of binary tree
关于跨域问题
PHP WebShell Free Kill
自定义mvc框架复习(crud)