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<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
void rev(char a[]){
int len = strlen(a);
for(int i = ; i < len / ; i++)
swap(a[i],a[len - - i]);
}
int main(){
int N, minL = , tag = , len = ;
char str[][], sen[];
scanf("%d", &N);
getchar();
for(int i = ; i < N; i++){
gets(str[i]);
rev(str[i]);
if(minL > strlen(str[i]))
minL = strlen(str[i]);
}
for(int i = ; i < minL; i++){
char c = str[][i];
for(int j = ; j < N; j++)
if(str[j][i] != c){
tag = ;
break;
}
if(tag == )
break;
len++;
}
if(len != )
for(int j = len - ; j >= ; j--)
printf("%c", str[][j]);
else
printf("nai");
cin >> N;
return ;
}

总结:

1、本题即求公共后缀,由于每个字符串长度不一,可以在输入字符串后逆序,转换为求公共前缀。

2、关于字符串的处理,本题无法使用scanf的%s读入字符串,因为字符串中存在空格。所以可以用gets(),在遇到回车时结束。但要注意,在scanf读入N后,还存在一个回车键,需要被吸收掉,否则会导致gets()读到一个回车空串。

3、由于PAT不支持gets()读入带空格的一行。以后遇到复杂的字符串题时,使用string而不是char[]。

最新文章

  1. ASP.NET Core 中文文档 第三章 原理(4)路由
  2. 贝塔阶段html及pdf模块测试
  3. python 装饰器的理解
  4. 使用Java判断字符串中的中文字符数量
  5. [React] Set up React apps with zero configuration
  6. 使用int?来确保值类型也可以为null
  7. ActionScript 3.0日期与时间管理(Date类)
  8. 使用VS插件在VS2012/2013上编辑和调试Quick-Cocos2d-x的Lua代码
  9. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
  10. Javascript的那些硬骨头:作用域、回调、闭包、异步……
  11. iOS NSInteger 的输出 %d %ld %zd %ld (long)
  12. Abd学习笔记
  13. 蚂蚁金服mPaaS 3.0发布 助力客户智能化构建超级App生态
  14. glide:4.7.1 与 26.1.0冲突
  15. matlab运行中出现“Caught &quot;std::exception&quot; Exception message is: Message Catalog MATLAB:builtins was not loaded from the file.&quot;
  16. 对于devexpress gridview 内插图加加进度条等的一点解读
  17. 【转】C#异步的世界【上】
  18. 用eclipse创建maven的webapp项目
  19. linux与unix时间戳互转
  20. coreos 创建使用密钥登陆的ubuntu 基础镜像

热门文章

  1. (代码篇)从基础文件IO说起虚拟内存,内存文件映射,零拷贝
  2. ANSYS附加动水质量(westergarrd公式)
  3. C_数据结构_快速排序
  4. 《Linux内核设计与实现》读书笔记——第四章
  5. java_web—JSP+Servlet+JavaBean
  6. Python学习笔记(一)——初学Python
  7. &quot;留拍&quot;-注册/登录详解
  8. docker+redis 持久化配置(AOF)
  9. Windows samba history
  10. HTML5 Base64_encoding_and_decoding