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进行截取答案。

最新文章

  1. java获取日期之间天数的方法
  2. javascript运行机制
  3. (转载)两种方法让HashMap线程安全
  4. Android APK 签名 (转发)
  5. (Struts)ActionForm类及表单数据验证
  6. poj 2891 扩展欧几里得迭代解同余方程组
  7. Linux内核学习笔记——内核内存管理方式
  8. 关于解压覆盖IIS文件后,新的文件不具备权限导致DMS系统无法正常运行
  9. C# 清楚Cookies
  10. SQL获取数据库名,表名,列名,说明等信息
  11. SOURCES的文件格式
  12. oracle 行转列 分析函数
  13. solr-1.4.1 环境配置
  14. Android学习小Demo一个显示行线的自定义EditText
  15. Web工程师的工具箱
  16. 年度钜献,108个大数据文档PDF开放下载
  17. win7 64位安装 oracle 11G 和 使用 PLSQL Developer 连接服务器
  18. 解题报告 HDU1159 Common Subsequence
  19. 深入理解 Spring 事务原理【转】
  20. ubuntu下 编译Caffe的Matlab接口

热门文章

  1. 使用asp.net调用谷歌地图api
  2. svn &amp; git 问题汇总
  3. google cloud之查看任务任务过程
  4. m2014_c-&gt;c语言容器类工具列
  5. 1853: [Scoi2010]幸运数字[容斥原理]
  6. 四 Android Studio打包EgretApp (热更新)
  7. 【BZOJ2049,2631,3282,1180】LCT模板四连A
  8. Zabbix使用SMTP发送邮件报警并且制定报警内容
  9. Python全栈day13(作业讲解根据用户输入选择输出字典内容)
  10. UVA10870—Recurrences(简单矩阵快速幂)