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. nsq 从搭建到应用(更新ing)windows

    1.官网下载地址 我安装的是windows nsq-1.2.0.windows-amd64.go1.12.9.tar.gz https://nsq.io/deployment/installing.h ...

  2. 文献阅读 - MonoLoco与关于Camera Matrix的笔记

    目录 概览 HighLights Camera Intrinsic Matrix 笔记 Intrinsic Matrix Task-Error - 不确定性任务下确界的计算 输出假设的Laplace分 ...

  3. 启动运行python3时 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 170: illegal multibyte sequence

    重现 在cmd中输入Python,运行后,出现以下错误: Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...

  4. 新发布的廉价版iPhoneXR值得购买吗?

    每年苹果九月份的发布会,都是一场声势浩大的全民狂欢"大趴体".其中最兴奋的,自然就是各路段子手--就指望着苹果发布会来提供一年的笑料呢!而今年苹果发布会召开之后,网上最流行的就是下 ...

  5. ORACLE自增函数,一般函数

    1.UNIX时间与普通时间互相转换 1.ORACLE先创建函数方法,再直接使用,MySQL直接使用方法UNIX_TIMESTAMP,FROM_UNIXTIME oracle_to_unix(creat ...

  6. 新浪sae url rewrite(伪静态、重定向)详解

    新浪sae url rewrite(伪静态.重定向)详解 http://www.veryhuo.com phpclubs 2011-11-14 投递稿件 sae全程Sina App Engine,真是 ...

  7. Java线程——线程池概念

    什么是线程池? 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间.那么有没有一种办法使得线程可以复用, ...

  8. 吴裕雄--天生自然MySQL学习笔记:MySQL 数据类型

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这 ...

  9. 【前缀思想】二叉树中所有距离为 K 的结点

    863. 二叉树中所有距离为 K 的结点 class Solution { Map<TreeNode,String>map=new HashMap<>(); String pa ...

  10. Ctrl +c 脚本中

    #!/bin/bashsar -n DEV 1 111111111111111 >>1.txt &   #实时网卡流量数据  sleep 3 && kill -2 ...