codeforces868D Huge Strings

You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed, on each of them you concatenate two existing strings into a new one. On the i-th operation the concatenation saisbi is saved into a new string sn + i (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer k such that all possible strings consisting of 0 and 1 of length k (there are 2k such strings) are substrings of the new string. If there is no such k, print 0.

Input

The first line contains single integer n (1 ≤ n ≤ 100) — the number of strings. The next n lines contain strings s1, s2, ..., sn (1 ≤ |si| ≤ 100), one per line. The total length of strings is not greater than 100.

The next line contains single integer m (1 ≤ m ≤ 100) — the number of operations. m lines follow, each of them contains two integers aiabd bi (1 ≤ ai, bi ≤ n + i - 1) — the number of strings that are concatenated to form sn + i.

Output

Print m lines, each should contain one integer — the answer to the question after the corresponding operation.

Example
input
5
01
10
101
11111
0
3
1 2
6 5
4 4
output
1
2
0
Note

On the first operation, a new string "0110" is created. For k = 1 the two possible binary strings of length k are "0" and "1", they are substrings of the new string. For k = 2 and greater there exist strings of length k that do not appear in this string (for k = 2 such string is "00"). So the answer is 1.

On the second operation the string "01100" is created. Now all strings of length k = 2 are present.

On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.

题意:给定n个01字符串,进行m次操作,每次将Sa,Sb,两个字符串合并成为新串Sn+i

求:对于每个新和成的字符串,存在数字k,满足长度为k的01字符全排列都存在于新和成的字符串的子串中,询问k的最大值

字符串 a 与 b 合并为字符串 ab,得到的 k(ab) 要么为 k(a),要么为 k(b),要么就是新的更大的数。 如果是新的数,那就肯定是合并的中间部分产生的。

也就是说,当前的字符串 s,如果它在后面还能有新的贡献,那就只能是两端与其它字符串合并产生的。 所以如果字符串长度过大,我们可以直接删去中间部分,因为中部部分在后面是不会产生贡献了

实际发现,k的范围其实很小(不知道为什么)

在k很小的情况下,暴力枚举全排列再判断就行

如果要k比较大,似乎有分治解法?

但网上很多题解都写了(1<<k),而且是int,据我所知,这个肯定会爆

然而都AC了,说明有某种结论(或者数据太水)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 string s[501];
 7 int n,m,ans[501];
 8 int zyys(int x)
 9 {int i,j,l;
10     for (i=1;i<=10;i++)
11     {
12         for (j=0;j<(1<<i);j++)
13         {
14             string myys;
15             for (l=0;l<i;l++)
16             if (j&(1<<l)) myys+='1';
17             else myys+='0';
18         if (s[x].find(myys)==-1) return i-1;
19         }
20     }
21 }
22 int main()
23 {int i,x,y;
24   cin>>n;
25   for (i=1;i<=n;i++)
26   {
27       cin>>s[i];
28   }
29   cin>>m;
30   for (i=n+1;i<=n+m;i++)
31   {
32       scanf("%d%d",&x,&y);
33       s[i]=s[x]+s[y];
34       if (s[i].length()>1000) 
35       s[i]=s[i].substr(0,500)+s[i].substr(s[i].length()-500,500);
36       ans[i]=max(ans[x],max(ans[y],zyys(i)));
37       printf("%d\n",ans[i]);
38   }
39 }

 

posted @ 2017-10-15 16:18  Z-Y-Y-S  阅读(337)  评论(0编辑  收藏  举报