1077 Kuchiguse (20 分)

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

题目大意:给出m个句子,判断所有句子的最长后缀。

//有空格怎么办?那就不是一个character了。。。

#include <iostream>
#include <algorithm>
#include<cstdio>
#include <vector>
#include<cstring>
#include<string.h>
#include<string>
using namespace std; int main()
{
int n;
cin>>n;
string pe="",now;//那就以第一个作为标准,剩下的去和它作比较。
int j=,k=,mins=j;
for(int i=;i<n;i++){//如何读取一行,真的是不会。。。。
getline(cin,now);
if(pe==""){
pe=now;continue;
}
for(j=pe.size()-,k=now.size()-;k>=,j>=;j--,k--){
if(pe[j]!=now[k]){
break;
}
}
if(j<mins){
mins=j;
}
}
if(mins==){
cout<<"nai";
}else{
for(int i=mins;i<pe.size();i++){
cout<<pe[i];
}
} return ;
}

//本来是写成这个样子,根本就不行啊,不过整行读取字符串的函数写的是对的。

根据柳神的修改了之后的,可以AC了

#include <iostream>
#include <algorithm>
#include<cstdio>
#include <vector> using namespace std; int main()
{
int n;
cin>>n;
string pe="",now,ans;//那就以第一个作为标准,剩下的去和它作比较。
getline(cin,now);
for(int i=;i<n;i++){//如何读取一行,真的是不会。。。。
getline(cin,now);
reverse(now.begin(),now.end());
if(i==){
ans=now;
}else{
int j=;
for(j=;j<ans.size()&&j<now.size();j++){
if(ans[j]!=now[j])break;
}
ans=ans.substr(,j);
}
}
reverse(ans.begin(),ans.end());
if(ans=="")
cout<<"nai";
else
cout<<ans;
return ;
}

1.使用reverse进行倒转,因为是比较后缀,不太好比较,最后的答案需要再翻转过来。

2.关于这个getline(cin,now)读取一行的问题,在输入了n之后,其实就有一个\n被getline给读取进来了,所以这样就浪费了一个,需要单独加一个getline来消灭\n才对!

3.使用substr进行截取答案。

PAT 1077 Kuchiguse [一般]的更多相关文章

  1. PAT 1077 Kuchiguse

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  2. pat 1077 Kuchiguse(20 分) (字典树)

    1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  3. PAT甲1077 Kuchiguse【字符串】【暴力】【Hash】【二分】

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  4. 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

    题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...

  5. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  6. 1077 Kuchiguse (20 分)

    1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Personal ...

  7. PAT 1077. 互评成绩计算

    PAT 1077. 互评成绩计算 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个 ...

  8. PAT 甲级 1077 Kuchiguse

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

  9. PAT Advanced 1077 Kuchiguse (20 分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

随机推荐

  1. 树链剖分 + 后缀数组 - E. Misha and LCP on Tree

    E. Misha and LCP on Tree Problem's Link Mean: 给出一棵树,每个结点上有一个字母.每个询问给出两个路径,问这两个路径的串的最长公共前缀. analyse: ...

  2. KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Problem's Link: http://poj.org/problem?id=2752 Mean: 给你一个字符串,求这个字符串中有多少 ...

  3. jQuery开发中容易忽视的错误

    1.引用jQuery库文件的<script>标签,必须放在引用自定义脚本文件的<script>标签之前,否则,就会发生找不到对象:最好在<head>元素中,把引入样 ...

  4. [Cxf] cxf 相关知识整理

    ① 请求方式为GET @GET @Path(value = "/userAddressManage") @Produces( { MediaType.APPLICATION_JSO ...

  5. 哪一个不是EL定义的隐式对象?(选择1项)

    哪一个不是EL定义的隐式对象?(选择1项) A cookie B.pageContext C.attributes D initParam 解答:C 1)pageContext:JSP 页的上下文.它 ...

  6. 敏捷软件开发实践-Code Review Process(转)

    介绍: 在敏捷软件开发中,从代码的产生速度上来看,要比 传统Waterfall产生速度高很多.因为我们把时间安排的更加紧凑了.那么这么多的代码,如何能保证这些代码质量呢?很多人可能直接想到静态代码检测 ...

  7. hdu 5065 数学题

    #include<iostream> #include<cmath> #include<cstdio> using namespace std; int A,B,y ...

  8. zabbix服务器搭建

    zabbix服务器源码安装 参看官方文档 这里不做过多的翻译,我的系统是centos6.5,安装的时候是base安装,所以要装一些其他依赖包,除此之外,还有一些php插件: extension=bcm ...

  9. hdu 3085(双向bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 思路:双向广搜,每次从M出发,搜三步,从G出发,搜一步,然后就是判断是否走到对方已经走过的格子, ...

  10. 经验总结20--C#模拟WEB请求

    非常多语言能够使用代码进行WEB请求,获取到须要的数据. 方便调用别人的接口,自己进行处理. HttpWebRequest request = WebRequest.Create(url) as Ht ...