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<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int N;
	int ans=0;
	scanf("%d",&N);
	int minlen = 256;
	char a[101][300];
	getchar();
	for(int i=0;i<N;i++)
	{
		fgets(a[i],256,stdin);
		int len = strlen(a[i]);
		if(minlen>len) minlen =len;
		for(int j=0;j<minlen/2;j++)//reverse
		{
			char temp;
			temp = a[i][j];
			a[i][j]=a[i][-j+len-1];
			a[i][-j+len-1]=temp;
		}
	}
	for(int i=1;i<minlen;i++)
	{
		bool flag=true;
		char c = a[0][i];
		for(int j = 1;j<N;j++)
		{
			if(c!=a[j][i])
			{
				flag = false;
				break;
			}
		}
		if(flag) ans++;
		else break;
	}
	if(ans)
	{
		for(int i=ans-1;i>=1;i--)
		{
			printf("%c",a[0][i]);
		}

	}
	else
	{
		printf("nai");
	}

	return 0;

}

感觉有点混乱

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
 	int n;
 	scanf("%d\n", &n);
 	string ans;
 	for(int i = 0; i < n; i++) {
 		string s;
 		getline(cin, s);
		 int lens = s.length();
 		reverse(s.begin(), s.end());
		 if(i == 0) {
 				ans = s;
		 		continue;
		 }
        else {
 				int lenans = ans.length();
				int minlen = min(lens, lenans);
 				for(int j = 0; j < minlen; j++) {
 						if(ans[j] != s[j]) {
 						ans = ans.substr(0, j);
 						break;
 						}
		 			}
		 		}
 }
 reverse(ans.begin(), ans.end());
 if (ans.length() == 0)
     ans = "nai";
cout << ans;
return 0;
}

以上的解法更加简便

使用到的新鲜函数是:getline, reverse, min

1. getline

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

字符串的输入方式之一,特点是遇到空格不停止输入

2. reverse

反转范围中元素的顺序[first,last)

3. min

输出两数中较小的一个,同理max是较大那个

PAT甲级——1077.Kuchiguse(20分)的更多相关文章

  1. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  2. PAT Advanced 1077 Kuchiguse (20 分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  3. PAT 甲级 1035 Password (20 分)(简单题)

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  4. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  5. 【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

    题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. AAAAAccepted code: #include<bits/stdc++. ...

  6. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  7. PAT 甲级 1077 Kuchiguse

    https://pintia.cn/problem-sets/994805342720868352/problems/994805390896644096 The Japanese language ...

  8. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  9. PAT甲级——1005.SpellItRight(20分)

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

随机推荐

  1. libevent简介

    一.参考资料 1.ubuntu下安装libevent 2.libevent实现TCP 服务端

  2. JAVAEE 和项目开发(第四课:HTTP的响应格式和响应状态码)

    HTTP 协议之响应 响应格式的结构: 响应行(状态行):HTTP 版本.状态码.状态消息 响应头:消息报头,客户端使用的附加信息 空行:响应头和响应实体之间的,必须的. 响应实体:正文,服务器返回给 ...

  3. 语句:{% url menu.url_name %}的作用

    语句: {% url menu.url_name %} 表示跳转,上面的menu.url_name意思是menu表的url_name字段 如果menu表的url_name字段的值是sales_dash ...

  4. Relu激活函数的优点

    Relu优点: 1.可以使网络训练更快. 相比于sigmoid.tanh,导数更加好求,反向传播就是不断的更新参数的过程,因为其导数不复杂形式简单. 2.增加网络的非线性. 本身为非线性函数,加入到神 ...

  5. CKeditor上传图片 实现所见即所得界面

    迟了好多天的分享,CKeditor这个编辑器虽然不错,但也真苟啊,搞图片上传这个功能,快给我搞佛系了,话不多说,上代码 1.首先去官网下载一个full的版本,我用的是CKeditor 4.13,解压之 ...

  6. redis(六)---- 简单延迟队列

    延迟队列的应用场景也很常见,例如:session的超时过期.自动取消未付款订单等等.redis中有一种数据结构叫做zset,即有序集合.元素类型为String类型,且元素具有唯一性不能重复,每个元素可 ...

  7. 基于图灵api的Python机器人

    一.注册图灵机器人 先注册并登录图灵机器人官网: 点击创建机器人 复制机器人的key 二.搭建Python机器人 Python版本:3.6 注意替换第三行代码的apikey import reques ...

  8. Eclipse中常用的快捷键总结!不收藏后悔!

    Eclipse中常用的快捷键总结!不收藏后悔!Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ ...

  9. 给adobe acrobat reader 添加图片注释

    首先展示一下  我添加注释的结果, 下面是我的做法: 在Adobe Acrobat 中打开Sample.pdf文档,点开文档右边的“工具”-“内容”-选择“编辑对象”,鼠标光标变成实心箭头+右下角小方 ...

  10. 74)搭建TCP服务器

    补充: 1-->listen是监听,就是监听你建立的那个socket那个窗口,要是有客户端来了,那么就把他加到 队列里面,然后accept是从队列中取客户端(就是把对应的客人的信息拿过来,交给w ...