zoukankan      html  css  js  c++  java
  • 字典树

    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 0x3f3f3f3f
    
    const 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:
    %d %d
    ", sign++, ans2, ans1);
            }
        }
        return 0;
    }
     
  • 相关阅读:
    selenium之WebDriver API
    python开发之面试题
    python开发之协程
    Python爬虫
    Python基础
    Django-搭建win7虚拟环境-virtualenv
    Linux系列
    Python知识点
    Python知识点
    Python基础-生物信息:找出基因,生物学家使用字母A、C、T和G构成的字符串建模一个基因组。
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5759022.html
Copyright © 2011-2022 走看看