[HNOI2004] Language L Language


Punctuation appear later appear in the text, so the previous language is no punctuation. Now you have to deal with is that no punctuation section of the article.
A passage T is composed of several lower case letters. W is a word composed of a number of lower-case letters. A dictionary D is a collection of several words.
We call T a passage in a dictionary D can be understood, means that every part of the word is in the dictionary D T if the article can be divided into several parts, and.
E.g. dictionary D includes the word { 'is', 'name' , 'what', 'your'}, the article 'whatisyourname' is in the dictionary D may be appreciated
that it can be divided into four words: 'what', 'is',' your ', ' name ', and each word belongs to the dictionary D, the article' whatisyouname '
can not be understood in a dictionary D, but can be understood in the dictionary D at' = D + { 'you' } . This article is a prefix 'whatis', can also be understood in the dictionary D
and is the longest prefix in the dictionary D can be understood.
Given a dictionary D, you need to determine whether the program several sections of the article can be understood in the dictionary D.
And give its location at the longest prefix dictionary D can be appreciated.
Input
input file is the first line of two positive integers n and m, the dictionary D has n words, and there needs to be processed article segment m.
After row n is described one word per line, m lines after each line describing a passage again.
Where 1 <= n, m <= 20, word length does not exceed 10 each, each length of the article does not exceed 1M.
Output
For each input section of the article, you need to output the position of the longest prefix of this article in the dictionary D can be understood.
The Input the Sample
. 4. 3
IS
name
What
your
whatisyourname
whatisyouname
whaisyourname
the Sample the Output
14
. 6
0
// the whole article 'whatisyourname' can be understood
// whatisyouname prefix 'whatis' can be understood
// whaisyourname no prefix can be understood

The Trie + DP

We just need to do a like existence dp

For each string, we set f [i] indicates whether bit i from 1 to be exact match

First f [0] = 1, then we have [i] = 1 we can match down for each f

Specific matching method is naturally to throw Trie tree,

I starting from this, in the event of a closing tag will mark the end position corresponding to the f [x] = 1, then it

#include<cstdio>
#include<cstring>
#include<iostream>
#define re register
#define maxn 1000005
char S[maxn];
int son[505][27],flag[505];
char T[11];
int cnt;
bool f[maxn];
inline int read()
{
    char c=getchar();
    int x=0;
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9')
      x=(x<<3)+(x<<1)+c-48,c=getchar();
    return x;
}
int n,m;
inline void ins()
{
    int len=strlen(T+1);
    int now=0;
    for(re int i=1;i<=len;i++)
    {
        if(!son[now][T[i]-'a']) 
		    son[now][T[i]-'a']=++cnt;
        now=son[now][T[i]-'a'];
    }
    flag[now]=1;
}
inline void check(int x,int len)
{
    int now=0;
    for(re int i=x;i<=len;i++)
    {
        if(!son[now][S[i]-'a'])
		   return;
        now=son[now][S[i]-'a']; 
	
        if(flag[now]) 
		// If this bit is a word ending position, then I can match to No. 
		  f[i]=1;
    }
}
int main ()
{
    n=read();m=read();
    for(re int i=1;i<=n;i++)
    {
        scanf("%s",T+1);
        ins();
    }
    for(re int t=1;t<=m;t++)
    {
        scanf("%s",S+1);
        memset(f,0,sizeof(f));
        f[0]=1;
        int len ​​= strlen (S + 1);
        int years = 0;
        for(re int i=0;i<=len;i++)
        // enumeration starting position 
        {
            if(!f[i]) 
			    continue;
            ans = i; // i bits can be matched prior to 
            check (i + 1, len); // run about from i + 1 to the second position to place the TRIE Len 
        }
        if(!ans) puts("-1");
        else printf("%d",ans),putchar(10);
    }
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/cutemush/p/12588943.html