Problem Description Ladies and gentlemen, please sit up straight.
Don’t tilt your head. I’m serious.
For
n given strings
S1,S2,⋯,Sn, labelled from
1 to
n, you should find the largest
i (1≤i≤n) such that there exists an integer
j (1≤j<i) and
Sj is not a substring of
Si.
A substring of a string
Si is another string that occurs
in
Si. For example, “ruiz” is a substring of “ruizhang”, and “rzhang” is not a substring of “ruizhang”.
Input The first line contains an integer
t (1≤t≤50) which is the number of test cases.
For each test case, the first line is the positive integer
n (1≤n≤500) and in the following
n lines list are the strings
S1,S2,⋯,Sn.
All strings are given in lower-case letters and strings are no longer than
2000 letters.
Output For each test case, output the largest label you get. If it does not exist, output
−1.
Sample Input
4 5 ab abc zabc abcd zabcd 4 you lovinyou aboutlovinyou allaboutlovinyou 5 de def abcd abcde abcdef 3 a ba ccc
Sample Output
Case #1: 4 Case #2: -1 Case #3: 4 Case #4: 3
题意:给n个字符串,编号1到n,找最大的编号j,使得存在1到 j-1 的某个串非 j 的字串
思路:kmp判断是否是字串,从小到大遍历,用i,j指针
#include<stdio.h>
#include<vector>
#include<queue>
#include <cstring>
using namespace std;
#define move(a) (1<<(a))
const int N = 1010;
const int inf = 0x3fffffff;
char s[510][2100];
int nextt[2100];
int kmp(char *s1,char *s2){
int i,j,len1,len2;
len1=strlen(s1);
len2=strlen(s2);
i=0;j=-1;
memset(nextt,-1,sizeof(nextt));
while(i<len2){
if(j==-1 || s2[i]==s2[j]){
i++;
j++;
nextt[i]=j;
}
else j=nextt[j];
}
i=0;j=0;
while(i<len1 && j<len2){
if(j==-1 || s1[i]==s2[j]){
i++;
j++;
}
else j=nextt[j];
}
if(j>=len2)
return 1;
else return 0;
}
int T,n;
int main()
{
int cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",&s[i]);
}
int ans=-1;
int i=1,j=2;
while(i<=n && j<=n)
{
while(i<j&&kmp(s[j],s[i])) i++;
if(i<j) ans=j;
j++;
}
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}
TLE的代码:(思路是 比较上有点繁琐)
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL ;
const LL mod=1000000009LL;
const int N=10000;
const int inf=0x3fffffff;
int T;
int n,m,k;
char s[2100];
char sta[510][2100];
int Next[2010][510];
int slen[510];
int tot=0;
void MakeNext(char p[],int len,int tot)
{
Next[0][tot]=-1;
int i=0;
int j=-1;
while(i<len)
{
if(j==-1||p[i]==p[j])
{
i++,j++;
if(p[i]!=p[j]) Next[i][tot]=j;
else Next[i][tot]=Next[j][tot];
}else
{
j=Next[j][tot];
}
}
}
int KMP(char T[],int N,char P[],int M,int tot)
{
int i=0,j=0;
while(i<N&&j<M)
{
if(T[i]==P[j]||j==-1) i++,j++;
else j=Next[j][tot];
}
if(j==M) return i-M+1;
else return -1;
}
int main()
{
int cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
scanf("%s",sta[0]);
tot=0;
slen[0] = strlen(sta[0]);
MakeNext(sta[0],slen[0],tot);
tot++;
int ans=-1;
for(int t=1; t<n; t++)
{
scanf("%s",&s);
int len = strlen(s);
int old = tot,i;
for(i=0; i<old; i++)
{
if(KMP(s,len,sta[i],slen[i],i)==-1)
{
MakeNext(s,len,tot);
for(int j=0;j<len;j++)
sta[tot][j]= s[j];
slen[tot++]=len;
ans=t+1;
break;
}
}
if(i==old)
{
tot=0;
MakeNext(s,len,tot);
for(int j=0;j<len;j++)
sta[tot][j]= s[j];
slen[tot++]=len;
}
// for(int i=0;i<tot;i++)
// {
// printf(" %s\n",sta[i]);
// }
}
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}