Source: Light OJ 1114 easily readable
Test instructions: How many compositions of a sentence can be made only to meet the first and last characters of each word and the middle order will change
Idea: Each word is sorted in addition to the middle character and then inserted into the dictionary tree to record the number of each word
Enter a sentence each word is also sorted after the search by multiplication principle answer is the product of the number of each word
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int maxnode = 111111;const int sigma_size = 52;int ch[maxnode][sigma_size];int val[maxnode];int sz;void init () {s z = 1;memset (ch[0],-1, sizeof (ch[0));} int idx (char c) {if (C >= ' a ' && c <= ' z ') return C-' a '; Elsereturn C-' a ' + 26;} void Insert (char *s) {int u = 0, n = strlen (s), for (int i = 0; i < n; i++) {int c = idx (S[i]), if (ch[u][c] = = 1) {memset (ch [SZ],-1, sizeof (Ch[sz])); Val[sz] = 0;ch[u][c] = sz++;} U = ch[u][c];} val[u]++;} int find (char *s) {int u = 0, n = strlen (s), for (int i = 0; i < n; i++) {int c = idx (s[i]); u = ch[u][c];if (Val[u] = = 1) re Turn 0;} return val[u];} int main () {int cas = 1;int t;scanf ("%d", &t), while (t--) {init (); int n;scanf ("%d", &n); for (int i = 0; i < n; i++) {char s[11111];scanf ("%s", s), int len = strlen (s), if (Len > 2) sort (s+1, s+len-1); Insert (s);} scanf ("%d", &n), GetChar ();p rintf ("Case%d:\n", cas++) and while (n--){int sum = 1;char S[11111];gets (s), char *p = strtok (S, ""), while (p) {char str[11111];strcpy (str, p); int len = strlen (str); if (Len > 2) sort (str+1, str+len-1), Sum *= find (str);p = Strtok (NULL, "");} printf ("%d\n", Sum);}} return 0;}
Light OJ 1114 easily readable dictionary tree