PTA (Advanced Level)1077.Kuchiguse
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的更多相关文章
- PAT (Advanced Level) 1077. Kuchiguse (20)
最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...
- 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 ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- 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 ...
- PTA (Advanced Level) 1008 Elevator
Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 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 ...
随机推荐
- nodejs的npm命令无反应的解决方案
这二天用npm下载模块的时候输入npm命令完全无反应,不是加载的那种状态而是下标不停地在哪里闪...之后找解决方案,说要删除npmrc文件.强调:不是nodejs安装目录npm模块下的那个npmrc文 ...
- python并发——从线程池获取返回值
并发是快速处理大量相似任务的绝佳办法,但对于有返回值的方法,需要一个容器专门来存储每个进程处理完的结果 from multiprocessing import Pool import time #返回 ...
- docker部署TX-LCN5.0.2
主要是在配置tx-lcn.manager.host时需要特别注意下,应该安装如下配置进行 tx-lcn: manager: host: 0.0.0.0 port: 8070 heart-time: 1 ...
- Qt Delgate的使用 简单说明
(一) Qt Model/View 的简单说明 .预定义模型 (二)使用预定义模型 QstringListModel例子 (三)使用预定义模型QDirModel的例子 (四)Qt实现自定义模型基于QA ...
- ip效验和ip段的效验
package com.juchen.utils; import javax.servlet.http.HttpServletRequest; /** * ip 相关的工具方法 */ public ...
- springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)
使用springboot之前,我们发送http消息是这么实现的 我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springbo ...
- android data binding jetpack VIII BindingConversion
android data binding jetpack VIII BindingConversion android data binding jetpack VII @BindingAdapter ...
- 【MyBatis】实现in操作符在WHERE 子句中规定多个值
Mapper.xml中写: <select id="selectIdsByDate" resultType="java.lang.Long"> se ...
- 4.Java JSON使用
Java 中 JSON 的使用 分类 编程技术 本章节我们将为大家介绍如何在 Java 语言中使用 JSON. 类库选择 Java中并没有内置JSON的解析,因此使用JSON需要借助第三方类库. 下面 ...
- ios-tableViewcell展开与收缩动画处理
[前言] 在使用华尔街见闻 app 时,看到它的 tableVeiw 上的 cell 具有很好的展开与收缩功能.于是自己想了一下实现,感觉应该挺简单的,于是心痒痒写个 demo 实现一波.华尔街见闻 ...