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. invisble

    不可见索引概念: 不可见索引(Invisible Index)是ORACLE 11g引入的新特性.不可见索引是会被优化器忽略的不可见索引,除非在会话或系统级别上将OPTIMIZER_USE_INVIS ...

  2. 十八、CI框架之数据库操作update用法

    一.代码如图: 二.访问一下 三.我们来查看数据库,已经被修改了 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢.

  3. Oracle学习笔记(2)

    2.Oracle用户管理 (1)创建用户:create user 用户名 identified by 密码(需要dba权限); sql>create user yzw identified by ...

  4. shell计数

    cat 20171015_datarecord.txt| awk -F '_' '{a[$1]++} END {for (i in a) {print i,a[i]|"sort -k 2&q ...

  5. The full stack trace of the root cause is available in the Apache Tomcat/8.0.8 logs.

    这个问题是版本冲突的问题 1.调低jdk 版本,不能让jdk版本太高,至少不能比tomcat高,要不然就会有这个错误. 2.如果看过我这篇博客的人(https://www.cnblogs.com/CH ...

  6. 云平台发展前沿报告 微软云平台——Windows Azure

    微软云平台——Windows Azure Windows Azure 是微软研发的公有云计算平台.该平台可供企业在互联网上运行应用,并可进行扩展.通过Windows Azure,企业能够在多个数据中心 ...

  7. FZU_1683 矩阵快速幂 求和

    这个题目确实是很简单的一个矩阵快速幂,但是我在求和的时候,用的是标准的求和,即,一共计算logN次Ak,但是这样会超时. 后来就发现原来本身和Sn=Sn-1+Fn:即Sn本身可以写在矩阵当中,所以直接 ...

  8. 华为荣耀magic book(锐龙版)安装ubuntu系统

    荣耀magic book锐龙版性价比很高,前段时间在朋友推荐下我自己也入手了一台.机器整体感觉不错,续航时间长(办公.无线上网5-6小时吧),速度快,买的时候4300,现在已经降到4000以下了,也算 ...

  9. web前端学习 roadmap

    <tag attribute="value">content</tag>

  10. Delphi调用c++写的dll (me)

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...