P2292 90pts HELP!
xmx20101223 · · 个人记录
#include<bits/stdc++.h>
using namespace std;
int n,m,ch[2010][26],tot=0,ans;
char s[2000100];
bool f[2000100],bo[2000100];
int read()
{
char ch=getchar();
long long x=0,w=1;
while(ch<'0'||ch>'9')
{
if(ch=='-')w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-48,ch=getchar();return w==1?x:-x;
}
void insert()
{
int u=0,len=strlen(s+1);
for(int i=1;i<=len;i++)
{
char c=s[i]-'a';
if(!ch[u][c]) ch[u][c]=++tot;
u=ch[u][c];
}
bo[u]=1;
}
int work()
{
int ans=0,len=strlen(s+1);
f[0]=1;
for(int i=0;i<=len;i++)
{
if(f[i]!=1) continue;else ans=i;
for(int u=0,j=i+1;j<=len;j++)
{
int c=s[j]-'a';
u=ch[u][c];
if(!u) break;
if(bo[u]) f[j]=1;
}
}
return ans;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
insert();
}
for(int i=1;i<=m;i++)
{
scanf("%s",s+1);
memset(f,0,sizeof(f));
printf("%d\n",work());
}
return 0;
}
include<bits/stdc++.h>
using namespace std;
int n,m,ch[2010][26],tot=0,ans;
char s[2000100];
bool f[2000100],bo[2000100];
int read()
{
char ch=getchar();
long long x=0,w=1;
while(ch<'0'||ch>'9')
{
if(ch=='-')w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-48,ch=getchar();return w==1?x:-x;
}
void insert()
{
int u=0,len=strlen(s+1);
for(int i=1;i<=len;i++)
{
char c=s[i]-'a';
if(!ch[u][c]) ch[u][c]=++tot;
u=ch[u][c];
}
bo[u]=1;
}
int work()
{
int ans=0,len=strlen(s+1);
f[0]=1;
for(int i=0;i<=len;i++)
{
if(f[i]!=1) continue;else ans=i;
for(int u=0,j=i+1;j<=len;j++)
{
int c=s[j]-'a';
u=ch[u][c];
if(!u) break;
if(bo[u]) f[j]=1;
}
}
return ans;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
insert();
}
for(int i=1;i<=m;i++)
{
scanf("%s",s+1);
memset(f,0,sizeof(f));
printf("%d\n",work());
}
return 0;
}