Problem Description
> The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet's might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in the x -th row and the y -th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x -th row and y -th row), at the same time the substring is a prefix of any other row (can be the x -th or the y -th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query ( x , y ), you should find the longest substring that have a risk of causing chain reaction.
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in the x -th row and the y -th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x -th row and y -th row), at the same time the substring is a prefix of any other row (can be the x -th or the y -th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query ( x , y ), you should find the longest substring that have a risk of causing chain reaction.
Input
The first line of the input contains an integer
T
, denoting the number of test cases.
For each test cases, the first line contains integer n ( n≤105 ).
There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105 .
And an integer m ( 1≤m≤100 ) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y , denoting the query.
For each test cases, the first line contains integer n ( n≤105 ).
There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105 .
And an integer m ( 1≤m≤100 ) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y , denoting the query.
Output
You should output the answers for the queries, one integer per line.
Sample Input
1 3 aaa baaa caaa 2 2 3 1 2
Sample Output
3 3
不知道别人的思路是怎么样的,这个题目比赛的时候想到了一些思路,可惜只有半个小时左右才想到的,时间来不及,而且我自己也不确定我的做法是否正确,就没写。。。
第二天早上,我决定写一个试一试,结果一遍就过了,unbelievable!!!!
介绍一下我的思路:对于每个字符串,前缀一个一个开始,能匹配就慢慢递增,找到最大的能匹配的。
具体说一下匹配过程:基于kmp算法,但是每次匹配的字符串长度会改变,就要求你不断求next的值,因此,在kmp求next值的时候做了一点改动,其中两个值k,j是不断改变的,用到了c++里面的引用,然后每次匹配到的话就返回匹配到的位置,然后匹配字符串的长度加1,在刚才的那个匹配的位置继续匹配,直到不能匹配。
本来还想保存一次以前查询的答案来着,结果就过了,所以还可以再优化一部分的。
贴一下代码:
如果没有看懂的我的代码部分,请结合kmp算法看一下:从头到尾理解字符串匹配kmp算法
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define ll long long
#define read(a) scanf("%d",&a);
using namespace std;
const int maxn=1e5+5;
string s[maxn];
int Next[maxn];
void getNext(int num,int &j,int &k,int len){
while(j<len-1){
if(k==-1||s[num][j]==s[num][k]){
++k;
++j;
Next[j]=k;
}
else{
k=Next[k];
}
}
}
int kmp_search(int x,int j,int lenx,int y,int i,int leny){
while(i<leny&&j<lenx){
if(j==-1||s[y][i]==s[x][j]){
i++;
j++;
}
else{
j=Next[j];
}
}
if(j==lenx)
return i-j;
else
return -1;
}
int search(int x,int y){
int len=0;
Next[0]=-1;
int j=0;
int k=-1;
int i=0;
int maxnlen=0;
while(len<s[x].size()){
len++;
getNext(x,j,k,len);
i=kmp_search(x,0,len,y,i,s[y].size());
if(i==-1){
break;
}
else{
maxnlen=max(maxnlen,len);
}
}
return maxnlen;
}
int main(){
freopen("test.txt","r",stdin);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
//cout<<s[i]<<endl;
}
int m;
cin>>m;
int ans=0;
while(m--){
int x,y;
cin>>x>>y;
if(x>y)
swap(x,y);
// ans=max(search(x,y),search(y,x));
// printf("%d\n",ans);
ans=0;
for(int i=1;i<=n;i++){
if(i!=x&&i!=y){
int num=min(search(i,x),search(i,y));
ans=max(num,ans);
}
}
ans=max(ans,search(x,y));
ans=max(ans,search(y,x));
printf("%d\n",ans);
}
}
return 0;
}