字典树 - 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
2
4
cocochannel
chrisschannel
MBCchannel
controlpanel
5
superman
batman
ironman
chrissbrown
MyCrown
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:\n%d %d\n", sign++, ans2, ans1);
}
}
return 0;
}
字典树 - A Poet Computer的更多相关文章
- ACM: Gym 100935F A Poet Computer - 字典树
Gym 100935F A Poet Computer Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- Barty's Computer 字典树
https://nanti.jisuanke.com/t/17122 Barty have a computer, it can do these two things. Add a new stri ...
- 字典树(Trie树)实现与应用
一.概述 1.基本概念 字典树,又称为单词查找树,Tire数,是一种树形结构,它是一种哈希树的变种. 2.基本性质 根节点不包含字符,除根节点外的每一个子节点都包含一个字符 从根节点到某一节点.路径上 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
随机推荐
- Web编程基础--HTML、CSS、JavaScript 学习之课程作业“仿360极速浏览器新标签页”
Web编程基础--HTML.CSS.JavaScript 学习之课程作业"仿360极速浏览器新标签页" 背景: 作为一个中专网站建设出身,之前总是做静态的HTML+CSS+DIV没 ...
- Container View 使用小技巧
一.传值,顺传 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { TVC *vc = segue.destin ...
- CSS3中的动画功能(二)
上一篇文章讲解了css3动画中的一个即transitions,那么今天来说说另外一个animations.和transitions不同的是animations可以定义多个关键帧以及每个关键帧中元素的属 ...
- 淘宝分布式NOSQL框架:Tair
Tair 分布式K-V存储方案 tair 是淘宝的一个开源项目,它是一个分布式的key/value结构数据的解决方案. 作为一个分布式系统,Tair由一个中心控制节点(config server)和一 ...
- 浅谈JavaScript中的defer,async
引言 开始重读<<JavaScript高级程序设计>>一书,看到关于JavaScript中关于defer.async的部分.网上查询了点资料,觉得蛮好的.现在总结下. defe ...
- ab 轻量的压测工具
阅读:http://www.cnblogs.com/luminji/archive/2011/09/02/2163525.html
- 使用Java 多线程编程 让三个线程轮流输出ABC,循环10次后结束
简要分析: 要求三个线程轮流输出,这里我们要使用一个对象锁,让关键部分的代码放入同步块当中.同时要有一个变量记录打印的次数到达10次循环后不再打印,另外一个就是要给每个线程一个标志号,我们根据标识号来 ...
- tyvj1172 自然数拆分Lunatic版
背景 话说小小鱼看了P1171(自然数拆分)之后感觉异常不爽,于是异常邪恶地将题目加强. 描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入格式 输入只有一个整数n ...
- C# 发送电子邮件
网上找到的发送邮件的类,改了一点点,在此谢谢原作者的奉献. 1.源码: public class CSendMail { private MailMessage mailMessage; privat ...
- PHP之static静态变量详解(二)
在看别人项目过程中,看到函数里面很多static修饰的变量,关于static修饰的变量,作用域,用法越看越困惑,所以查了下资料. static用法如下: 1.static 放在函数内部修饰变量 2.s ...