Codeforces 868 D. Huge Strings 字符串思维乱搞
2017-10-07 19:41
295 查看
传送门: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;
}
题意:有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;
}
相关文章推荐
- CodeForces 732D Exams (思维+set乱搞)
- Codeforces-868B ,C 模拟,思维。。
- Codeforces 264B Good Sequences【dp+思维】
- Codeforces 821D Okabe and City【思维建图+Dij+优先队列优化】好题~好题~
- CodeForces - 724C Ray Tracing 扩展欧几里得+思维
- Codeforces 439D Devu and his Brother【思维+三分查找】
- CodeForces 11B Jumping Jack(思维)
- codeforces 560 C. Gerald's Hexagon (思维,几何)
- Codeforces 469C 24 Game【思维+模拟】
- codeforces 804A Find Amir 思维/水题
- CodeForces 723C-Polycarp at the Radio(模拟 贪心 vector乱搞)
- 【CodeForces】554B - Ohana Cleans Up(思维)
- codeforces 466B Wonder Room(思维,暴力)
- codeforces 868D 分治哈希
- codeforces-141【B找规律】【C思维】
- Codeforces 155C Hometask【思维+Dp】
- 【CodeForces - 638】C 【思维+DFS】
- Codeforces-448【A数学函数ceil】【B思维】【D二分】
- Codeforces 675C Money Transfers 思维题
- CodeForces - 427A (警察和罪犯 思维题)