Dictionary Tree-A poet Computer

Source: Internet
Author: User

The ACM team is working on a AI project called (Eih Eye Three) that allows computers to write poems. One of the problems they stumbled upon is finding words with the same suffix. The ACM team constructed a dictionary of words, they was interested only in the longest common suffix, which is, a suffix c Ommon to three or more words in the dictionary ... A suffix is any substring this starts from some arbitrary position in the string and reaches the end of the string. As the ACM team is also preparing for the acm-tcpc2015 contest, they figured that the contestants can help in solving thi s problem. Your task is to write a program, finds a longest common suffix in a dictionary of words. An entry in the dictionary is a word of 中文版 letters only. Small letters is the same as capital letters. You can assume this there is exactly a unique solution for every test case.

Input

The first line of the input contains an integer T, the number of test cases. Each test case starts with a line containing one integer K, then K lines follow, each containing one string "Si" that repr Esents an entry in the dictionary. 0 < T ≤50 | S I|≤100 0 < K ≤1000

Output

For each test case, print on the first line "Case C:" Where ' C ' was the test case number. On the second line should print an integer denoting the length of the longest common suffix and another integer denoti ng How many words has the suffix appeared in.

Sample Input

Input
2
4
Cocochannel
Chrisschannel
Mbcchannel
ControlPanel
5
Superman
Batman
Ironman
Chrissbrown
Mycrown
Output
Case 1:
7 3
Case 2:
7 ·


--------------------------------------------------------------I'm the split line ^_^----------------------------------------- -----------------


A dictionary tree topic, at that time still want to use the pointer to make out, unfortunately there is no time, but after the game looked at someone else's code, feel direct
Open structure array analog pointer also good, although a lot of waste of memory, but fast, it is worth learning, previously written pointer to the dictionary tree, sleep
It's really going to make people dizzy = =, so learn the dictionary tree of the structure array version, which is almost the same as the pointer version, slightly
Change on the line, the main body basically the same, and slowly understand later .... Just remember this routine.

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<cctype>UsingNamespace Std;#define INT __int64#define INF0x3f3f3f3fConstint MAXN=111111;int triecnt;int root;struct Node{int End;Represents the number of strings that are formed by thisint son[26];int deep;The first character of a string}trie[MAXN];IntNew_tire(){triecnt++; Trie[triecnt]. End=0;For(int I=0; I<26; I++){Trie[triecnt].son[I]=0;}Return triecnt;}voidInit_trie(){triecnt=0; Root=New_tire();}voidInsert_str(Char Str[]){int Len=Strlen(str);int RT= root;For(int I= Len-1; I>=0; I--){int ID= str[I]-A;If(trie[RT].son[ID]==0){Trie[RT].son[ID]=New_tire(); Rt= Trie[RT].son[ID]; Trie[RT]. End++; Trie[RT].deep= Len-I;}Else{RT= Trie[RT].son[ID]; Trie[RT]. End++; Trie[RT].deep= Len-I;}}}IntMain(){Freopen ("Input.txt", "R", stdin);int CAs;int sign;While(scanf("%d",&cas)! = EOF){Sign=1;While(CAS--){Init_trie();int k;scanf("%d",&k);While(k--){Char Str[105];scanf('%s 'Str);int Len=Strlen(str);For(int I=0; I< Len; I+ +) str[I]=ToLower(str[I]);Insert_str(str);}int ans1=0, Ans2=0;For(int I=1; I<= triecnt; I++){If(trie[I]. End>=3&& trie[I].deep> Ans2){ans1= trie[i. End= trie[i ].deep;  } printf (  "case%d:\n%d%d\n" ++, Ans2 } return 0               /span>             

Dictionary Tree-A poet Computer

Related Article

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.