ACM: Gym 100935F A Poet Computer - 字典树
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
/*/
题意:找最长常见后缀; 单纯的字典树,找后缀数大于等于3,长度尽可能大的后缀。 AC代码:
/*/
#include"algorithm"
#include"iostream"
#include"cstring"
#include"cstdlib"
#include"cstdio"
#include"string"
#include"vector"
#include"queue"
#include"cmath"
using namespace std;
typedef long long LL ;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))
#define FK(x) cout<<"["<<x<<"]\n" struct Trie {
int v;
int len;
Trie *next[26];
} root; struct Ans {
int len,num;
} ans,t; void init() {
ans.len=ans.num=0;
t.len=t.num=0;
} void BuildTree(char *s) {
// FK("NO");
int len=strlen(s);
Trie *p=&root,*q;
for(int i=len-1; i>=0; i--) {
int num;
// if(!((s[i]<='Z'&&s[i]>='A')||(s[i]<='z'&&s[i]>='a')))continue;
if(s[i]<='z'&&s[i]>='a')num=s[i]-'a';
else num=s[i]-'A';
if(p->next[num]==NULL) {
q=(Trie *)malloc(sizeof(root));
q->v=1;
for(int j=0; j<26; j++) {
q->next[j]=NULL;
}
q->len=p->len+1;
p->next[num]=q;
p=p->next[num];
} else {
p=p->next[num];
p->v++; }
if(p->v >= 3&&p->len >= t.len) {
t.len=p->len;
t.num=p->v;
}
}
} void DeleteTrie(Trie *T,int k) {
if (T==NULL) return ;
for(int i=0; i<26; i++) {
if(T->next[i]!=NULL) {
DeleteTrie(T->next[i],k+1);
}
}
if(k==0) {
for(int i=0; i<26; i++) {
T->next[i]=NULL;
}
} else free(T);
return ;
} int main() {
int T,n;
char s[205];
scanf("%d",&T);
for(int qq=1; qq<=T; qq++) {
scanf("%d",&n);
init();
for(int i=0; i<n; i++) {
cin>>s;
BuildTree(s);
if(t.num>=3&&t.len>=ans.len) {
ans=t;
}
}
printf("Case %d:\n", qq);
printf("%d %d\n",ans.len,ans.num);
Trie *p=&root;
DeleteTrie(p,0);
}
return 0;
}
ACM: Gym 100935F A Poet Computer - 字典树的更多相关文章
- [ACM] hdu 1251 统计难题 (字典树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- Barty's Computer 字典树
https://nanti.jisuanke.com/t/17122 Barty have a computer, it can do these two things. Add a new stri ...
- ACM之路(15)—— 字典树入门练习
刷的一套字典树的题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120748#overview 个人喜欢指针的字典树写法,但是大力 ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- ACM:统计难题 解题报告-字典树(Trie树)
统计难题 Time Limit:2000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Submit Status ...
- A .Gaby And Addition (Gym - 101466A + 字典树)
题目链接:http://codeforces.com/gym/101466/problem/A 题目: 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 思路: ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n, ...
- codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
随机推荐
- CI重定向:php(codeigniter)中如何重定向
Q: 在保存完数据之后需要重定向,防止数据重复提交. 我使用$this->方法名();跳转,发现不能达到重定向的效果(地址栏没变) 请教高手重定向怎么用 A: $this->load-&g ...
- ASP.NET MVC 伪静态的实现
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...
- Myeclipse 2016 & 2014 下载
myeclipse-2016-ci-6-offline-installer-windowshttps://downloads.genuitec.com/downloads/myeclipse/inst ...
- sdut 487-3279【哈希查找,sscanf ,map】
487-3279 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接: sdut: http://acm.sdut.ed ...
- Could not link against boost_system 解决办法
Could not link against boost_system 解决办法: 先安装 libboost-all-dev ./configure --with-incompatible-bdb - ...
- POJ3691 DNA repair(AC自动机 DP)
给定N个长度不超过20的模式串,再给定一个长度为M的目标串S,求在目标串S上最少改变多少字符,可以使得它不包含任何的模式串 建立Trie图,求得每个节点是否是不可被包含的串,然后进行DP dp[i][ ...
- HDU1899 Sum the K-th's(树状数组)
枚举,每次增加点,删除点 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...
- Win10 UI线程
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => UpdateButtonOrientation());
- 单链表带头结点&不带头结点
转自:http://blog.csdn.net/xlf13872135090/article/details/8857632 Node *head; //声明头结点 带头结点初始化 void I ...
- 解决 CentOS网卡eth0启用不了问题
转自:http://www.centoscn.com/CentosBug/osbug/2014/0423/2850.html [root@localhost Desktop]# service net ...