比賽之字典樹題解

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   os   io   

這道題第一眼看見題目所給的時間就有一種預感,僅僅是600ms,啟動並執行演算法複雜度稍微高一點就會逾時。那麼我首先是犯傻想偷偷懶,直接是調用一個系統庫函數strstr(),希望它能夠完成自己的題目,但是顯然是逾時的。百度了一下它的實現方法是直接採用沒有最佳化的演算法,複雜度是最高的。但是由於自己壓根就不會寫字典樹,所以還是抱著一個僥倖的心態去用KMP演算法來實現,結果還是鐵鐵的逾時。那麼最後的實現應該是通過什麼方式呢?

很顯然,這道題是一個很裸的字典樹題,直接使用字典樹的方式解決是最好的。以後也要將這些最基本的演算法牢牢地記在心中,不然真的到了比賽的時候就可能要鐵鐵的後悔了。

題目:

A - Substring ProblemTime Limit:600MS     Memory Limit:0KB     64bit IO Format:%lld & %lluSubmit Status

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代碼:

#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 ;}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.