PAT A1077 Kuchiguse (20分)

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

题意:

输入固定行数的字符串(包括空格),找出每行中最后几个字符都相同的字符串,如果存在这个共同字符串就输出,不存在就输出nai.

思路:

(1)设定bool型变量yes,判断其值为true或false输出共同字符串或nai;
(2)开数组a[],b[],c[],Ku[],数组a、b依次存放前一个、后一个字符串,从第一行字符串开始遍历,c存放每两行字符串中最后几个字符都相同的字符串,一旦c为空则yes值为false,最后输出nai,如果c不为空则将每次获得的c赋值给Ku,最后获得的Ku则为共同字符串.

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main(){
	bool yes = true;
	int N,count=0;
	char a[260],b[260],c[260],Ku[260];
	scanf("%d\n",&N);
	for(int i=0;i<N;i++){
		if(i%2==0){
			scanf("%[^\n]",&a);//加\n表示键入的值以换行结束 
			getchar();//吃掉上一个回车符,不然下次输入会出错 
			reverse(a,a+strlen(a));
		}else{
			scanf("%[^\n]",&b);
			getchar();
			reverse(b,b+strlen(b));
		}
		if(i>0){
			for(int j=0;j<strlen(a);j++){
				if(a[j]==b[j]){
					c[count]=a[j];
					count++;
				}else{
					break;
				}
			}
			c[count] = {'\0'};
			if(count!=0){
					int k=0;
					for(k;k<count;k++){
						Ku[k] = c[k];
					}
					Ku[k] = {'\0'};
				count = 0;
			}else{
				yes = false;
			}
		}
	}
	if(yes==false)
		printf("nai");
	else{
		reverse(Ku,Ku+strlen(Ku));
		printf("%s",Ku);	
	}
	return 0;
}

词汇:

notorious 臭名显著的
preference 偏好
Anime 动漫
Manga 漫画
stereotype 固定模式
suffix 后缀

发布了26 篇原创文章 · 获赞 0 · 访问量 475

猜你喜欢

转载自blog.csdn.net/PanYiAn9/article/details/103914573