Bzoj 1212: [Hnoi2004]l language (DP + trie)

Source: Internet
Author: User
Tags bitset

Because the words are short ... Use trie then every time DP violence find ... It should also be possible to use hash +dp ....

---------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <bitset>using namespace std; #define C (x) ((x)-' a ')const int MAXN = 1100000;const int MAXV = n;const int c = n;int N, Q;Char S[MAXN];bitset<maxn> F;struct Node {Node *ch[c];bool F;Node (): F (0) {memset (CH, 0, sizeof ch);}} POOL[MAXV], *root, *pt;void Init_trie () {pt = pool;Root = pt++;}void Init () {scanf ("%d%d", &n, &q);Init_trie ();for (int i = 0; i < N; i++) {scanf ("%s", s);node* t = Root;for (int i = 0, n = strlen (s); i < n; i++) {if (!t->ch[c (s[i)))t->ch[c (s[i])] = pt++;t = t->ch[c (s[i]);}t->f = true;}}void Work () {While (q--) {F.reset ();scanf ("%s", S + 1);int n = strlen (s + 1);f[0] = 1;for (int i = 0; i < n; i++) if (F[i]) {node* t = Root;For (int j = i + 1; t->ch[c (S[j])]; j + +) {t = t->ch[c (s[j]);if (t->f) f[j] = 1;}}for (int i = n; I >= 0; i--) if (F[i]) {printf ("%d\n", I);Break ;}}}int main () {

Init ();Work ();return 0;}

---------------------------------------------------------------------------

1212: [Hnoi2004]l language Time Limit:Ten Sec Memory Limit:162 MB
Submit:984 Solved:412
[Submit] [Status] [Discuss] Description

The appearance of punctuation is later than the appearance of text, so the previous language is no punctuation. Now all you have to deal with is an article with no punctuation. A section of article T is made up of several lowercase letters. A word w is also composed of several lowercase letters. A Dictionary of D is a collection of several words. We call an article T under a dictionary D to be understood, that is, if the article T can be divided into several parts, and each part is a word in dictionary d. For example dictionary D includes the word {' is ', ' name ', ' What ', ' your '}, then the article ' Whatisyourname ' can be understood under dictionary D because it can be divided into 4 words: ' What ', ' is ', ' your ', ' Name ', and each word belongs to dictionary d, and the article ' Whatisyouname ' cannot be understood under dictionary D, but can be understood under dictionary d ' =d+{' you '. A prefix ' whatis ' of this passage can also be understood under dictionary D and is the longest prefix that can be understood under dictionary D. Given a dictionary d, your program needs to determine whether several passages can be understood under dictionary D. and gives the position of the longest prefix that can be understood under dictionary D.

Input

The first line of the input file is two positive integers n and m, which indicates that there are n words in dictionary d, and that there is an M segment that needs to be processed. The next n lines describe one word per line, and then each row of M lines describes an article. Among them 1<=n, m<=20, each word length does not exceed 10, each paragraph length does not exceed 1M.

Output

For each article entered, you need to output the position of the longest prefix that the article can be understood in dictionary D.

Sample Input 4 3
Is
Name
What
Your
Whatisyourname
Whatisyouname
Whaisyourname
Sample Output 14
6
0 whole passage ' whatisyourname ' can be understood
The prefix ' Whatis ' can be understood
No prefix can be understood

HINT

Source

Dp string

Bzoj 1212: [Hnoi2004]l language (DP + trie)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.