当前位置:网站首页>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();
}
边栏推荐
猜你喜欢

# ODS及DWD层自动化构建##, 220731,

SSM网上商城购物系统(前台+后台)

MySQL8--Windows下使用msi(图形界面)安装的方法

MySQL8--Windows下使用压缩包安装的方法

2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)

MySQL8.0.26 installation and configuration tutorial (windows 64-bit)

MySQL8.0.26安装配置教程(windows 64位)

MySQL8.0.28 installation tutorial

MySQL中根据日期进行范围查询

Kubernetes 基本概念
随机推荐
深度自编码网络的集成学习ICPS入侵检测模型
MySQL index optimization in practice
程序员的七夕浪漫时刻
mysql8.0.28 download and installation detailed tutorial, suitable for win11
JSP Webshell 免杀
PHP WebSehll 后门脚本与检测工具
【遥控器开发基础教程3】疯壳·开源编队无人机-ADC(摇杆控制)
EF Core:基于关系的复杂查询 区分IEnumerable和IQueryable
MySQL8.0.26 installation and configuration tutorial (windows 64-bit)
2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)
一个资深测试工程师面试一来就问我这些题目
7-41 PAT排名汇总 (25 分)多样排序
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module ‘./scope‘
MySQL函数(经典收藏)
#{}和${}的区别
(转帖)HashCode总结(1)
合奥科技网络 面试(含参考答案)
嵌入式分享合集25
Kubernetes 基本概念
PHP WebShell 免杀