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<bits/stdc++.h>
using namespace std;
vector<string> v;
int main()
{
int n;
scanf("%d\n", &n); //注意要把换行符也读进来
int shortest = 500;
string s;
for(int i=0;i<n;i++)
{
getline(cin, s);
if(s.size() < shortest) shortest = s.size();
reverse(s.begin(), s.end());
v.push_back(s);
} bool right;
int index = 0;
for(int i=0;i<shortest;i++)
{
char ch = v[0][i];
right = true;
for(int j=1;j<n;j++)
{
if(v[j][i] != ch)
{
right = false;
break;
}
}
if(right) index++;
else break;
} if(index == 0)
cout << "nai";
else
for(int i=index-1;i>=0;i--)
cout << v[0][i];
return 0;
}
引用

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

PTA (Advanced Level)1077.Kuchiguse的更多相关文章

  1. PAT (Advanced Level) 1077. Kuchiguse (20)

    最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...

  2. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  5. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  6. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  7. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. PTA (Advanced Level) 1006 Sign In and Sign Out

    Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...

随机推荐

  1. MySQL基础练习---牛客网的数据以及典型题目

    1 部门表departments 部门no和部门名称 2 部门员工表 dept_emp 每个部门对应的员工信息 3 部门经理表 dept_manager 每个部门的经理信息 4 员工表 employe ...

  2. 洛谷P2622 关灯问题II

    洛谷题目链接 声明: 本篇文章不讲基础,对萌新不太友好,(我就是萌新),要学状压$dp$的请另寻,这篇文章只是便于本人查看.... 首先看到$n<=10$,就可以考虑状压了,要求最小值,所以初始 ...

  3. 获取link后的参数值

    getQueryString:function(name){ var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i ...

  4. python一些问题

    1.对于字符变量来说不需要深度复制,字符变量是不能改变的 2.文件读取结尾的判断是通过判读 line=self.fd.readline() if not line: //结束了 不用通过判断字符长度. ...

  5. Oil Deposits (HDU - 1241 )(DFS思路 或者 BFS思路)

    转载请注明出处:https://blog.csdn.net/Mercury_Lc/article/details/82706189作者:Mercury_Lc 题目链接 题解:每个点(为被修改,是#)进 ...

  6. mac使用php-version切换PHP版本

    在开发过程中,有时候我们的程序对某个php版本有着极为重要的限制,特别是大型项目. 因此,我们就需要切换多个php版本来满足我们的需求. 我们使用php-version来达到这个目的. 首先我们先使用 ...

  7. 在linux操作系统上进行简单的C语言源码的gcc编译实验

    尝试在linux上用gcc 而非封装完好的codeblocks,vs等ide 来编译c和cpp源程序 首先查看我的gcc版本,我的是VM centos 自带的,没有的话得自行安装,安装上gcc就可以在 ...

  8. 想学习linux操作系统,于是选择了在win8 虚拟机VM player 里装了Linux版本Centos7

    第一次接触linux,第一次玩linux的命令行哈. 以下python使用的都是自带的python2.x版本 先新建一个简单的文件夹py_studydir 新建该文件夹下面的一个py文件 写入pyth ...

  9. CodeForces 535C Tavas and Karafs —— 二分

    题意:给出一个无限长度的等差数列(递增),每次可以让从l开始的m个减少1,如果某个位置已经是0了,那么可以顺延到下一位减少1,这样的操作最多t次,问t次操作以后从l开始的最长0序列的最大右边界r是多少 ...

  10. conda虚拟环境 相关操作

    查询环境 conda env list 或者 conda info --envs 创建环境 conda create -n your_env_name python=3.7 删除环境 conda re ...