字典樹 - A Poet Computer

來源:互聯網
上載者:User

標籤:

The ACM team is working on an 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 are interested only in the longest common suffix, That is, a suffix common to three or more words in the dictionary… A suffix is any substring that starts from some arbitrary position in the string and reaches the end of the string. As the ACM team was also preparing for the ACM-TCPC2015 contest, they figured that the contestants can help in solving this problem. Your task is to write a program that finds a longest common suffix in a dictionary of words. An entry in the dictionary is a word of English letters only. Small letters are the same as capital letters. You can assume that there is exactly one 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 represents an entry in the dictionary. 0 < T ≤ 50 |Si| ≤ 100 0 < K ≤ 1000

Output

For each test case, print on the first line “Case c:” where ‘c’ is the test case number. On the second line you should print an integer denoting the length of the longest common suffix and another integer denoting how many words have 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:
3 3


--------------------------------------------------------------我是分割線^_^----------------------------------------------------------


一道字典樹的題目,當時還是想用指標做出來的,可惜沒有時間了,不過賽後看了一下別人的代碼,覺得直接
開結構體數組類比指標也蠻好的,雖然浪費很多記憶體,不過速度很快,值得一學,之前寫過指標的字典樹,覺
得指來指去真是會讓人頭暈= =,所以學習一下這個結構體數組版本的字典樹,其實和指標版的差不多,稍微
改改就行了,主體基本一樣的,以後慢慢理解.......記住這個套路就行

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<cctype>using namespace std;#define Int __int64#define INF 0x3f3f3f3fconst int MAXN = 111111;int triecnt;int root;struct Node {    int End;//代表以此結為的字串有多少個    int son[26];    int deep;//字串的第幾個字元}trie[MAXN];int new_tire() {    triecnt++;    trie[triecnt].End = 0;    for (int i = 0; i < 26; i++) {        trie[triecnt].son[i] = 0;    }    return triecnt;}void init_trie() {    triecnt = 0;    root = new_tire();}void insert_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;        }    }}int main() {    //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;                    ans2 = trie[i].deep;                }            }            printf("Case %d:\n%d %d\n", sign++, ans2, ans1);        }    }    return 0;}
 

字典樹 - A Poet Computer

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.