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 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
随机推荐
- Myeclipse 2016 & 2014 下载
myeclipse-2016-ci-6-offline-installer-windowshttps://downloads.genuitec.com/downloads/myeclipse/inst ...
- dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get
在64位的操作系统安装oracle10g 软件安装完成后,使用dbca建库的时候报下面的错: $ dbcaUnsatisfiedLinkError exception loading native l ...
- HDU2296 Ring(AC自动机 DP)
dp[i][j]表示行走i步到达j的最大值,dps[i][j]表示对应的串 状态转移方程如下: dp[i][chi[j][k]] = min(dp[i - 1][j] + sum[chi[j][k]] ...
- apache lucene solr 官网历史版本下载地址
官网上一般只提供最新版本的下载,下面两个链接为所有历史版本的下载地址: lucene地址:archive.apache.org/dist/lucene/java/ solr地址:archive.apa ...
- CentOS下Apache安装SSL
https是一个安全的访问方式,数据在传输过程中是加密的.https基于ssl. 一.安装apache和ssl模块1.安装apacheyum install httpd2.安装ssl模块yum ins ...
- C# 创建Windows Service
当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...
- WPF QuickStart系列之线程模型(Thread Model)
这篇博客将介绍WPF中的线程模型. 首先我们先来看一个例子,用来计算一定范围内的素数个数. XAML: <Grid> <Grid.RowDefinitions> <Row ...
- IIS常见错误
1.IIS7运行时访问报错,先安装VS,再启用的IIS,那么需要为IIS进行注册,在VS工具命令行中执行“aspnet_regiis.exe -ir -enable”即可 2.错误“未能加载文件或程序 ...
- hdu 4055 递推
转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...
- Android三种基本的加载网络图片方式(转)
Android三种基本的加载网络图片方式,包括普通加载网络方式.用ImageLoader加载图片.用Volley加载图片. 1. [代码]普通加载网络方式 ? 1 2 3 4 5 6 7 8 9 10 ...