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.
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.
Print m lines, each should contain one integer — the answer to the question after the corresponding operation.
5
01
10
101
11111
0
3
1 2
6 5
4 4
1
2
0
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了,说明有某种结论(或者数据太水)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
string s[];
int n,m,ans[];
int zyys(int x)
{int i,j,l;
for (i=;i<=;i++)
{
for (j=;j<(<<i);j++)
{
string myys;
for (l=;l<i;l++)
if (j&(<<l)) myys+='';
else myys+='';
if (s[x].find(myys)==-) return i-;
}
}
}
int main()
{int i,x,y;
cin>>n;
for (i=;i<=n;i++)
{
cin>>s[i];
}
cin>>m;
for (i=n+;i<=n+m;i++)
{
scanf("%d%d",&x,&y);
s[i]=s[x]+s[y];
if (s[i].length()>)
s[i]=s[i].substr(,)+s[i].substr(s[i].length()-,);
ans[i]=max(ans[x],max(ans[y],zyys(i)));
printf("%d\n",ans[i]);
}
}