字典树 - A Poet Computer

本文介绍了一个使用字典树寻找多个单词中最长公共后缀的问题,并提供了一种有效的解决方案。通过构建字典树并逆序插入字符串,可以找到具有最长共同后缀的单词数量及其长度。

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

转载于:https://www.cnblogs.com/steamedbun/p/5759022.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值