hdu 6096
来源:互联网 发布:java jsonarray 删除 编辑:程序博客网 时间:2024/05/21 17:56
String
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 702 Accepted Submission(s): 225
Problem Description
Bob has a dictionary with N words in it.
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.
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.
Input
The first line of the input gives the number of test cases T; T test cases follow.
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
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 (
Next Q line, each line has two string Pi , Si, represents the prefix and suffix of the ith word in the list (
All of the above characters are lowercase letters.
The dictionary does not contain the same words.
Limits
Output
For each test case, output Q lines, an integer per line, represents the answer to each word in the list.
Sample Input
14 4abacdeacdefacdefa acd efac ace f
Sample Output
2110若主字符串为"cde",将主字符串变成"cde#cde",若前缀为"ac",后缀为"a",则变为"a#ac".注意'#'为任意非'a'~'z'的字符即可。然后建立AC自动机,让主串去匹配AC自动机上的前后缀串。稍微改下AC自动机模板即可。#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cstdio>#include<cctype>#include<string>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;typedef long long LL;const int MX =1e6+1e5+7;char str[MX];char *s[MX];char s1[MX],s2[MX];int ans[MX],lenth[MX];struct AC_machine { int rear, root; int Next[MX][27], Fail[MX], End[MX], dl[MX],pos[MX]; void Init(){ rear = 0; root = New(); } int New() { rear++; End[rear] = 0; for(int i = 0; i < 27; i++){ Next[rear][i] = -1; } return rear; } void Add(char*A,int I) { int n = strlen(A), now = root; for(int i = 0; i < n; i++) { int id = A[i] - 'a'; if(Next[now][id] == -1) { Next[now][id] = New(); dl[rear] = i+1; } now = Next[now][id]; } End[now] = 1; pos[I] = now; } void Build(){ queue<int>Q; Fail[root] = root; for(int i = 0; i < 27; i++){ if(Next[root][i] == -1){ Next[root][i] = root; }else{ Fail[Next[root][i]] = root; Q.push(Next[root][i]); } } while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = 0; i < 27; i++) { if(Next[u][i] == -1){ Next[u][i] = Next[Fail[u]][i]; }else{ Fail[Next[u][i]] = Next[Fail[u]][i]; Q.push(Next[u][i]); } } } } void Query(char *S, int len){ int n = strlen(S), now = 1, ret = 0; for(int i = 0; i < n; i++) { now = Next[now][S[i] - 'a']; int temp = now; while(temp != root){ if(dl[temp] <= len) ans[temp]++; temp = Fail[temp]; } } }}AC;int main(){ int T,n,m; scanf("%d",&T); while(T--){ AC.Init(); scanf("%d%d",&n,&m); memset(str,0,sizeof(str)); memset(ans,0,sizeof(ans)); int j = 0,len; for(int i = 1; i <= n; i++){ s[i] = str+j; scanf("%s",s[i]); len = strlen(s[i]); lenth[i] = len+1; strcpy(s1,s[i]); s[i][len] = 'z'+1; strcat(s[i],s1); len = strlen(s[i]); s[i][len] = 0; j += len+1; } for(int i = 1; i <= m; i++){ scanf("%s%s",s1,s2); len = strlen(s2); s2[len] = 'z'+1; strcat(s2,s1); AC.Add(s2,i); len = strlen(s1); for(int j = 0; j < len; j++) s1[j] = 0; len = strlen(s2); for(int j = 0; j < len; j++) s2[j] = 0; } AC.Build(); for(int i = 1; i <= n; i++){ AC.Query(s[i],lenth[i]); } for(int i = 1; i <= m; i++){ printf("%d\n",ans[AC.pos[i]]); } } return 0;}
阅读全文
0 0
- hdu 6096
- hdu 6096
- hdu 6096 String
- hdu 6096 String(详解)
- hdu--6096--String
- HDU 6096-String
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- Javascript逻辑运算的返回值
- 【JavaSE学习笔记】IO流06_其他常用流(基本数据类型流、内存操作流、打印流、标准流、随机访问流、合并流、序列化与反序列化流)、属性集合类Properties
- POJ-1066Treasure Hunt(计算几何线段相交)
- Machine Learning7集成学习(一种高效的学习方法)
- 【Computer】第一次机房收费系统——配置流程(一)
- hdu 6096
- whoosh学习
- hdu6095 Rikka with Competition 2017多校第五场 贪心+思维
- CAS锁c源码实现
- PyQt编程之模态与非模态对话框(二)
- (UVA
- <javascript>简介和基本语法
- 欢迎使用CSDN-markdown编辑器
- java鬼混笔记:Hibernate:1、基本操作