hdu 2896 分类: hdu 2015-04-02 17:48 28人阅读 评论(0) 收藏
WA了好多次,注意
- 这题有多组数据
- bool强制转换int
#include<map>
#include<string>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
const int MAXN = 505, MAXL = 205, MAXS = 10005;
int n;
struct TireNode{TireNode *p[128],*fail; int end;}*root = 0, *p , emp = {{NULL},NULL,0};
TireNode *newnode(){TireNode *ret = (TireNode*)malloc(sizeof(TireNode)); (*ret) = emp; return ret;}
void Insert(int id,char str[])
{
TireNode *tmp = root;
int sl = strlen(str);
for(int j = 0; j < sl; j++)
{
if(!tmp->p[str[j]])
tmp->p[str[j]] = newnode();
tmp = tmp->p[str[j]];
}
tmp->end = id;
}
void TireBuild()
{
static char s[MAXL];
root = newnode();
for(int i = 1; i <= n; i++)
{
scanf("%s",s);
Insert(i,s);
}
}
void acBuild()
{
std::queue<TireNode*>line;
root->fail = root;
for(int i = 0; i < 128; i++)
if(!root->p[i])root->p[i] = root;
else
{
root->p[i]->fail = root;
line.push(root->p[i]);
}
while(!line.empty())
{
TireNode *now = line.front();
line.pop();
for(int i = 0; i < 128;i++)
if(!now->p[i])
now->p[i] = now->fail->p[i];
else
{
now->p[i]->fail = now->fail->p[i];
line.push(now->p[i]);
}
}
}
bool query(int CaseNum,char str[])
{
int len = strlen(str);
TireNode *now = root, *tmp;
bool flag = false;
static bool hash[MAXN];
memset(hash,false,sizeof(hash));
for(int i = 0 ;i < len ;i++)
{
now = now->p[str[i]];
tmp = now;
while(tmp != root)
{
if(tmp->end)
flag = hash[tmp->end] = true;
tmp = tmp ->fail;
}
}
if(!flag) return false;
printf("web %d:",CaseNum);
for(int i = 1 ;i <= n;i++)
if(hash[i]){printf(" %d",i);}
printf("\n");
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hdu2896.in","r",stdin);
freopen("hdu2896.out","w",stdout);
#endif
static char mus[MAXS];
while(scanf("%d",&n)!=EOF)
{
int sum = 0 ,T;
TireBuild();
acBuild();
scanf("%d",&T);
for(int i = 1; i <= T; i++)
{
scanf("%s",mus);
sum += (int)query(i,mus);
}
printf("total: %d\n",sum);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。