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. 017_linuxC++之_多态的引入

    多态:一种接口,多种方法(同一种调用方法,根据不同的对象,调用不同类中的函数)静态联编:非虚函数,在编译时确定好动态联编:   1. 对象里有指针,指向虚函数表 2. 通过指针,找到表,调用虚函数 3 ...

  2. (vue.js)Vue element tab 每个tab用一个路由来管理?

    (vue.js)Vue element tab 每个tab用一个路由来管理? 来源:网络整理     时间:2017/5/13 0:24:01     关键词:   关于网友提出的“ (vue.js) ...

  3. learning armbian steps(6) ----- armbian 源码分析(一)

    为了深入学习armbian,前面已经学习了如何手动构建arm ubuntu rootfs. 由于armbian官方的文档比较的匮乏,所以最终还是决定通过其编译的过程来深入地学习. 为了快速度深入地学习 ...

  4. [python]有中文字符程序异常的解决方案

    一. 含有中文字符无法运行 在python3中用的是Unicode编码,Unicode号称万国码,可以向所有的编码进行兼容.不会出现这种问题. Python2中使用的是ASCII编码,会出现这种问题. ...

  5. Python3 Address already in use 解决方法

    1.查看使用端口号netstat -ntlp 2.根据端口号找到pid 3.杀死程序 kill -9 pid 4.重新启动程序 简单粗暴 我使用python3时编写Socket,linux系统下使用c ...

  6. axios拦截器使用方法

    vue中axios获取后端接口数据有时候需要在请求开始时显示loading,请求结束后隐藏loading,这时候到每次调接口时都写上有点繁琐,有时候还会漏写. 这时候axios的拦截器就起了作用,我们 ...

  7. Redis订阅广播实现多级缓存

    Redis应用场景很多,现在介绍一下它的几大特性之一   发布订阅(pub/sub) 特性介绍: 什么是redis的发布订阅(pub/sub)?   Pub/Sub功能(means Publish, ...

  8. linux中wget未找到命令

    (转)linux中wget未找到命令   转:https://blog.csdn.net/djj_alice/article/details/80407769 在装数据库的时候发现无法使用wget命令 ...

  9. Linux设备驱动程序 之 read和write

    read和write原型 read和write方法完成的任务是相似的,亦即,拷贝数据到应用程序空间,或者反过来从应用程序空间拷贝数据:因此,它们的原型很相似,如下: ssize_t (*read) ( ...

  10. Arcengine获得arcgis安装的版本

    ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Version  //gsioracle MessageBox.Show(ESRI.ArcGIS.RuntimeMan ...