Light OJ 1114 easily readable dictionary tree

Source: Internet
Author: User

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

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.