比赛之字典树题解
这道题第一眼看见题目所给的时间就有一种预感,仅仅是600ms,运行的算法复杂度稍微高一点就会超时。那么我首先是犯傻想偷偷懒,直接是调用一个系统库函数strstr(),希望它能够完成自己的题目,但是显然是超时的。百度了一下它的实现方法是直接采用没有优化的算法,复杂度是最高的。但是由于自己压根就不会写字典树,所以还是抱着一个侥幸的心态去用KMP算法来实现,结果还是铁铁的超时。那么最后的实现应该是通过什么方式呢?
很显然,这道题是一个很裸的字典树题,直接使用字典树的方式解决是最好的。以后也要将这些最基本的算法牢牢地记在心中,不然真的到了比赛的时候就可能要铁铁的后悔了。
题目:
Description
String Matching is an important problem in computer science research and finds applications in Bioinformatics, Data mining,pattern recognition, Internet security and many more areas.
The problem we consider here is a smaller version of it. You are given a string M and N other strings smaller in length than M. You have to find whether each of these N strings is a substring of M. All strings consist of only alphanumeric characters.
You are required to write a C/CPP code to solve the problem.
Input
Input to the program consists of a series of lines. The first line contains the string M (no more than 100000 characters long). The next line contains an integer N (<1000) the number of query strings. Each of the next N lines contain a string S (each of which is no more than2000 characters long).
Output
Output should consist of N lines each with a character 'Y'/'N' indicating whether the string S is a substring of String M or not.
Sample Input
Input:
abghABCDE
2
abAB
ab
Output:
N
Y
Note: The test data for this problem not only consist of the official test cases from the contest,as well some cases of my own.
A testcase is added on 25.7.2010,after rejudging 3 users loose accepted.
参考AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn = 1000050 ; const int sigma_size = 52 ; int ID[1010] , tot ; char text[100050] , word[2111] ; bool flag[1010] ; int son[maxn][sigma_size] , val[maxn] , f[maxn] , last[maxn] , q[maxn], sz ; inline int idx(char c) { if(c<='Z') return c - 'A' ; else return c - 'a' + 26 ; } int Insert(char *s){ int u = 0 ; for(int i=0 ; s[i] ; i++) { int v = idx(s[i]) ; if(!son[u][v]) son[u][v] = ++sz ; u = son[u][v] ; } if(!val[u]) val[u] = ++tot ; return val[u]; } void get_fail() { int rear = 0 ; f[0] = 0 ; for(int c=0; c<sigma_size ; c++) { int u = son[0][c] ; if(u) f[u] = last[u] = 0 , q[rear++] = u ; } for(int _i=0; _i<rear ; _i++) { int u = q[_i] ; for(int c=0; c<sigma_size; c++){ int v = son[u][c] ; if(!v) { son[u][c] = son[f[u]][c] ; continue ; } q[rear++] = v; int x = f[u] ; while(x && !son[x][c]) x = f[x] ; f[v] = son[x][c] ; last[v] = val[f[v]] ? f[v] : last[f[v]] ; } } } void print(int u){ while(u) { flag[val[u]] = true ; u = last[u] ; } } void Find(char *s){ int j = 0; for(int i=0; s[i] ; i++) { int c=idx(s[i]); while(j && !son[j][c]) j = f[j] ; j = son[j][c] ; print(j) ; } } int main() { gets(text) ; int n ; scanf("%d", &n) ; getchar() ; for(int i=1; i<=n; i++) { scanf("%s" , word) ; ID[i] = Insert(word); } Find(text) ; for(int i=1; i<=n; i++) { if(flag[ ID[i] ]) puts("Y") ; else puts("N") ; } return 0 ; }