La 3942-remember the word

Source: Internet
Author: User

Question link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 1943

The exercise Prefix Tree (trie), also known as the dictionary tree.

Trie struct template:

 

Struct trie {int SZ; int ch [maxnode] [sigma_size]; int Val [maxnode]; void Init () {SZ = 1; memset (CH [0], 0, sizeof (CH [0]);} int idx (char c) {return C-'A';} void insert (char * s, int V) {int u = 0; int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! Ch [u] [c]) {memset (CH [SZ], 0, sizeof (CH [SZ]); Val [SZ] = 0; // do not write the following error: ch [u] [c] = SZ ++;} u = CH [u] [c];} Val [u] = V ;} int find (char * s) {int u = 0; int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! Ch [u] [c]) return 0; u = CH [u] [c];} return Val [u] ;}};

For this question, DP + trie can solve the problem.

 

Where d [I] represents the Decomposition Scheme ending with s [I-1.

 

# Include <stdio. h> # include <string. h> # include <math. h> # include <stdlib. h> # include <time. h> # include <vector> # include <set> # include <map> # include <algorithm> using namespace STD; # define maxnode 4010*100 # define sigma_size 26 # define mod 20071027 struct trie {int SZ; int ch [maxnode] [sigma_size]; int Val [maxnode]; void Init () {SZ = 1; memset (CH [0], 0, sizeof (CH [0]);} int idx (char c) {return C-'A';} Vo Id insert (char * s, int v) {int u = 0; int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! Ch [u] [c]) {memset (CH [SZ], 0, sizeof (CH [SZ]); Val [SZ] = 0; // do not write the following error: ch [u] [c] = SZ ++;} u = CH [u] [c];} Val [u] = V ;} int find (char * s) {int u = 0; int n = strlen (s); For (INT I = 0; I <n; I ++) {int c = idx (s [I]); If (! Ch [u] [c]) return 0; u = CH [u] [c];} return Val [u] ;}}; trie; char s [300005]; int d [300005]; // d [I] represents the Decomposition Scheme ending with s [I-1] int main () {# ifndef online_judge freopen ("in.txt", "r ", stdin); # endif int N; char temp [105]; int CAS = 0; while (scanf ("% s", S )! = EOF) {CAS ++; memset (D, 0, sizeof (d); D [0] = 1; trie. init (); scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% s ", temp); trie. insert (temp, 1);} int Len = strlen (s); For (INT I = 0; I <Len; I ++) {int u = 0; for (Int J = I + 1; j <= Len; j ++) {int c = trie. idx (s [J-1]); If (! Trie. ch [u] [c]) break; If (trie. val [trie. ch [u] [c]) {d [J] + = d [I]; If (d [J]> = mod) d [J]-= MOD ;} U = trie. ch [u] [c] ;}} printf ("case % d: % d \ n", Cas, d [Len]) ;}}

 

 

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.