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]) ;}}