浙大机试2014:Kuchiguse

http://www.patest.cn/contests/pat-a-practise/1077


The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)
  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

    Now given a few lines spoken by the same character, can you find her Kuchiguse?

    Input Specification:

    Each input file contains one test case. For each case, the first line is an integer N (2<=N<=100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

    Output Specification:

    For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write "nai".

    Sample Input 1:
    3
    Itai nyan~
    Ninjin wa iyadanyan~
    uhhh nyan~
    
    Sample Output 1:
    nyan~
    
    Sample Input 2:
    3
    Itai!
    Ninjinnwaiyada T_T
    T_T
    
    Sample Output 2:
    nai
    #include <stdio.h>
    #include <string.h>
    typedef struct{
    	char p[280];
    	int len;
    }SPOKE;
    SPOKE s[100];
    int main(){
    	int n,i,minlen,j;
    	char ch;
    	int mismatch = 0;
    	scanf("%d",&n);
    	getchar();
    	for(i = 0;i < n;i++){
    		gets(s[i].p);
    		s[i].len = strlen(s[i].p);
    	}
    	minlen = s[0].len;
    	for(i = 1;i < n;i++){
    		if(minlen > s[i].len)
    			minlen = s[i].len;
    	}
    	for(i = 1;i <= minlen;i++){
    		ch = s[0].p[s[0].len-i];
    		for(j = 1;j < n;j++){
    			if(s[j].p[s[j].len-i] != ch){
    				mismatch = 1;
    				break;
    			}
    		}
    		if(mismatch)
    			break;
    	}
    	if(i == 1)
    		printf("nai");
    	else
    		printf("%s",&s[0].p[s[0].len-i+1]);
    	return 0;
    }


  • 你可能感兴趣的:(算法,解题报告,浙大机试)