您的位置:首页 > 其它

Codeforces 868 D. Huge Strings 字符串思维乱搞

2017-10-07 19:41 288 查看
传送门:Codeforces 868 D

题意:有n个01字符串,第i个操作是用第l个和第r个拼成第n + i个字符串,然后询问最大的k使得所有长度为k的01串都在这个串中出现过。

思路:emmm,借鉴了杜瑜皓大佬的思路和代码风格,这题解法很多,前提是要知道一点:k的范围不会很大,甚至不会超过10。知道了答案范围这么小以后基本就可以瞎搞了,暴力,搜索,二分,STL什么的都能上。

大佬的思路是将字符串看成2进制数以后转成十进制,然后在bitset里标记出来,每次两个字符串进行拼接的话只会在中间拼接的地方产生新串,因此对于超过一定长度的串,我们只保存其固定长度的前后缀就好了,并且对每个串标记一下是否是只保存了前后缀,还是保存了整串。

注意字符串转十进制数的时候001和01是没有分别的,因此我们要在串最前面加一个1.

答案的求解就是暴力枚举,然后根据bitset判断一下就好了。

要注意的就是拼接的时候的处理方式(见代码)。

代码:

#include<bits/stdc++.h>
#define MAXN (1 << 17)
#define N 16
using namespace std;
int get_id(string s){
int sz = s.size(), ans = 0;
for(int i = 0; i < sz; i++)
ans = (ans << 1) | (s[i] & 1);
return (1 << sz) | ans;
}
struct str{
bitset<MAXN> bk;
string pre, suf;
bool all;
void update(string s)
{
int up = s.size();
for(int len = 1; len <= min(up, N); len++)
for(int i = 0; i + len <= up; i++)
bk[get_id(s.substr(i, len))] = 1;
}
void simply()
{
if(pre.size() > N) pre = pre.substr(0, N), all = 0;
if(suf.size() > N) suf = suf.substr(suf.size() - N, N), all = 0;
}
int query()
{
for(int i = N; i >= 0; i--)
{
int ok = 1, up = 1 << i;
for(int j = 0; j < up; j++)
if(!bk[up | j]){
ok = 0; break;
}
if(ok) return i;
}
return 0;
}
}ac[210];
void solve(int now, int l, int r)
{
ac[now].bk = ac[l].bk | ac[r].bk;
if(ac[l].all) ac[now].pre = ac[l].pre + ac[r].pre, ac[now].all = 1;
else ac[now].pre = ac[l].pre;
if(ac[r].all) ac[now].suf = ac[l].suf + ac[r].suf, ac[now].all = 1;
else ac[now].suf = ac[r].suf;
ac[now].update(ac[l].suf + ac[r].pre);
ac[now].simply();
}
string s;
int main()
{
ios::sync_with_stdio(0);
int n, m, l, r;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> s;
ac[i].update(s);
ac[i].pre = ac[i].suf = s;
ac[i].all = 1;
ac[i].simply();
}
cin >> m;
for(int i = 1; i <= m; i++){
cin >> l >> r;
solve(n + i, l, r);
cout << ac[n + i].query() << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: