Bzoj 1212 Hnoi 2004 L Language Trie tree

Source: Internet
Author: User

The main topic: give some words, and some sentences, when and only if the sentence can be divided into a substring can be translated by the dictionary, it means that the substring can be translated. The longest prefix length that can be translated.


Idea: Use the trie tree to brush the array, the longest can be brushed is the longest place to translate the string.

PS: On the Bzoj trie incredibly faster than AC automata, my slag code to brush to the first article ...


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std    struct trie{Trie *son[27];         BOOL End;        Trie () {memset (son,null,sizeof (son));    end = false; }}*root = new Trie (); int Words,cnt;char s[1 << 20|100],temp[20];bool f[1 << 20|100];    inline void Insert (char *s) {Trie *now = root;        while (*s! = ')} {if (Now->son[*s-' a '] = = NULL) now->son[*s-' a '] = new Trie ();        now = now->son[*s-' a '];    ++s; } Now->end = true;}    inline void Ask (char *s,int i) {Trie *now = root;    int t = 0;        while (*s! = ')} {if (Now->son[*s-' a '] = = NULL) return;        now = now->son[*s-' a '];        ++s;        ++t;    if (now->end) F[i + t] = true; } if (now->end) F[i + t] = true;}    inline int work () {memset (f,false,sizeof (f));    F[0] = true;    int re = 0,length = strlen (s + 1); for (int i = 0; I <= leNgth;        ++i) {if (!f[i]) continue;        re = i;    Ask (s + i + 1,i); } return re;}    int main () {cin >> words >> CNT;        for (int i = 1; I <= words; ++i) {scanf ("%s", temp);    Insert (temp);        } for (int i = 1; I <= cnt; ++i) {scanf ("%s", S + 1);    printf ("%d\n", Work ()); } return 0;}


Bzoj 1212 Hnoi 2004 L Language Trie tree

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.