ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT-A1077 Kuchiguse

2021-09-18 19:59:46  阅读:121  来源: 互联网

标签:case nyan include PAT 后缀 int Kuchiguse A1077


A1077 Kuchiguse (找字符串相同字符串)

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

题意:

输入N行字符串,寻找它们共同的后缀,如果存在共同的后缀,就输出共同的后缀;如果不存在,就输出nai

分析:

  1. 接收数值,注意换行符,数值类型,空格
  2. 寻找最小长度(为了之后的遍历),数组反转
  3. 遍历查找相同字符串,从in [0] [1] 到in [0] [minlen];如果不相等就直接结束循环,否则计时器count+1,继续遍历in [i] [i]…
  4. 如果计时器count=0,说明没有公共后缀,输出nai; 否则就遍历倒序输出公共后缀

注意点:

  • PAT不能用gets()来接收数据,如果遇到字符串中有空格的这种情况,最好用string类型,用getline(cin,数组名)来接收(头文件iostream和命名空间)
  • 接收换行符------getchar()
  • reverse的使用,数组反转。reverse(in[i].begin(),in[i].end());
  • 寻找相同部分的整个过程!!!

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
string in[256];//接收数据
int main(){
	int n,count=0;
	int minlen=256;
	scanf("%d",&n);
	getchar();//接收换行符 
	for(int i=0;i<n;i++){
	    //scanf("%s",&in[i]);因为有空格,不能能用scanf来接收
         getline(cin, in[i]);
        int len=in[i].size();
		if(len<minlen) minlen=len;
		//数组翻转
     	reverse(in[i].begin(),in[i].end()); //注意!!! 
	}
	
	//寻找相同
	int i,j;
	bool flag;
	for(i=0;i<minlen;i++){
		flag=true;
		for(j=1;j<n;j++){
			if(in[0][i]!=in[j][i]){
				flag=false;
				break;
			}
		}
		if(flag) count++;//字符串第i位相等,计时器count+1 
		else break;	
    }
	if(count==0) printf("nai");
	else{
		for(i=count-1;i>=0;i--){
			printf("%c",in[0][i]);
		}
	}	 
	return 0;
} 

标签:case,nyan,include,PAT,后缀,int,Kuchiguse,A1077
来源: https://blog.csdn.net/weixin_46055626/article/details/120373014

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有