hdu 6096---String(AC自动机)
2018-06-17 21:58:45来源:未知 阅读 ()
题目链接
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character.
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it could be 0. But the prefix and suffix of each word can not overlap.
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix.
There are probably many answers. You just have to figure out how many words may be the answer.
Each test case contains two integer N and Q, The number of words in the dictionary, and the number of words in the list.
Next N line, each line has a string Wi, represents the ith word in the dictionary (0<|Wi|≤100000)
Next Q line, each line has two string Pi , Si, represents the prefix and suffix of the ith word in the list (0<|Pi|,|Si|≤100000,0<|Pi|+|Si|≤100000)
All of the above characters are lowercase letters.
The dictionary does not contain the same words.
Limits
T≤5
0<N,Q≤100000
∑Si+Pi≤500000
∑Wi≤500000
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <string> #include <queue> #include <vector> using namespace std; const int N=1e5+5; string s[N]; struct Node{ Node *son[30]; Node *fail; int flag; int len; }tr[6*N]; Node *root; queue<Node*>Q; int ans[N]; vector<int>v[N]; int tot; void init() { tot=0; memset(ans,0,sizeof(ans)); root=&tr[0]; while(!Q.empty()) Q.pop(); for(int i=0;i<N;i++) v[i].clear(); for(int i=0;i<6*N;i++) { tr[i].flag=0; tr[i].fail=NULL; for(int j=0;j<30;j++) tr[i].son[j]=NULL; } } void build(string s,int id) { Node *now=root; for(int i=0;i<s.length();i++) { int x=s[i]-'_'; if(!now->son[x]) now->son[x]=&tr[++tot]; now=now->son[x]; } if(now->flag) { v[now->flag].push_back(id); return ; } now->flag=id; now->len=s.length(); } void setFail() { Q.push(root); while(!Q.empty()) { Node *now=Q.front(); Q.pop(); for(int i=0;i<30;i++) { if(now->son[i]) { Node *p=now->fail; while(p && (!(p->son[i]))) p=p->fail; now->son[i]->fail=(p)?p->son[i]:root; Q.push(now->son[i]); } else now->son[i]=(now!=root)?now->fail->son[i]:root; } } } void query(string s) { Node *now=root; int len=s.length(); for(int i=0;i<len;i++) { int x=s[i]-'_'; now=now->son[x]; Node *p=now; while(p!=root) { if(p->flag && p->len<=len/2+1) ans[p->flag]++; p=p->fail; } } } int main() { int T; cin>>T; while(T--) { init(); int n,q; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) { cin>>s[i]; s[i]=s[i]+"_"+s[i]; } for(int i=1;i<=q;i++) { string s1,s2; cin>>s1>>s2; string ss=s2+"_"+s1; build(ss,i); } setFail(); for(int i=1;i<=n;i++) { query(s[i]); } for(int i=1;i<=q;i++) ///处理相同的前后缀; { for(int j=0;j<v[i].size();j++) ans[v[i][j]]=ans[i]; } for(int i=1;i<=q;i++) printf("%d\n",ans[i]); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash