zoukankan      html  css  js  c++  java
  • ACM: Gym 100935F A Poet Computer

    Gym 100935F A Poet Computer
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    standard input/output

    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

    /*/
    题意:找最长常见后缀;
    
    单纯的字典树,找后缀数大于等于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<<"]
    "
    
    
    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:
    ", qq);
    		printf("%d %d
    ",ans.len,ans.num);
    		Trie *p=&root;
    		DeleteTrie(p,0);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    三种实现AJAX的方法以及Vue和axios结合使用的坑
    一个简陋的个人小项目,也是个人第一个真正意义上的独立项目——Graph
    使用docsify并定制以使它更强大
    使用particles.js实现网页背景粒子特效
    使用nginx和tomcat配置反向代理和动静分离
    php (zip)文件下载设置
    php 获取当前完整url地址
    php 实现重定向的三种方式
    php 查看使用多少内存
    linux 查看系统信息
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5759239.html
Copyright © 2011-2022 走看看