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 ...
随机推荐
- contents() 查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容
contents() V1.2概述 查找匹配元素内部所有的子节点(包括文本节点).如果元素是一个iframe,则查找文档内容 示例 描述:大理石平台检定规程 查找所有文本节点并加粗 HTML 代码 ...
- PHP mysqli_fetch_row() 函数
定义和用法 mysqli_fetch_row() 函数从结果集中取得一行,并作为枚举数组返回. <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con= ...
- 部分易错JS知识点整理(缓慢填坑)
主要还是各地搜刮来的,本人对于这方面的总结还是8彳亍,给各位大佬磕头了砰砰砰 1. 2.JS闭包和匿名对象以及作用域 js在执行之前,会将所有带var和function的进行提前定义和声明.(带var ...
- 【luoguP1955 】[NOI2015]程序自动分析--普通并查集
题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变 ...
- GIT上面有的分支,本地却无法检出,也看不到该分支
正常情况在gitlib上面可以看到代码里面有develop的分支 然而本地在查看所有分支的时候却报错 #查看所有的分支 git branch -a 这种情况是没有更新远程分支的索引,所以这样是看不到的 ...
- Linux Centos安装宝塔面板教程
材料:阿里云服务器远程ip地址和购买服务的账号和密码 版本Centos 6.9版本 用Xshell远程登录安装宝塔 Xshell的界面是这样的,如图 点击左上角新建回话可以创建一个新的SSH连接,如图 ...
- Django中三种方式写form表单
除了在html中自己手写form表单外,django还可以通过 继承django.forms.Form 或django.forms.ModelForm两个类来自动生成form表单,下面依次利用三种方式 ...
- Oracle 报错 ORA-03290的处置
MySql 的tuancate命令是直接truncate tableName,但在Oracle需要写成truncate table tableName,改正就好了. --END-- 2019.10.1 ...
- 7.Mahout菩萨
1.Maout简介 2.机器学习介绍 3.Mahout算法介绍
- <HTML>在一个表格内嵌套另一个表格时,如何居中?
在一个表格内嵌套另一个表格时,如何居中? 假设大表格为: <table id="tableRow"> <tr> <th>City</th& ...