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[]。

A1077. Kuchiguse的更多相关文章

  1. PAT甲级——A1077 Kuchiguse

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

  2. PAT A1077 Kuchiguse (20)

    晴神书中AC代码 #include <cstdio> #include <cstring> #include <iostream> using namespace ...

  3. PAT_A1077#Kuchiguse

    Source: PAT A1077 Kuchiguse (20 分) Description: The Japanese language is notorious for its sentence ...

  4. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  5. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

  6. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  7. 1077 Kuchiguse (20 分)

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

  8. 1077. Kuchiguse (20)

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

  9. PAT1077: Kuchiguse

    1077. Kuchiguse (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming The Japan ...

随机推荐

  1. Docker网络解决方案 - Calico部署记录

    简单来说,实现docker跨主机容器间通信,常用的第三方网络方案是Flannel,Weave,Calico:Flannel会为每个host分配一个subnet,容器从这个subnet中分配ip,这些i ...

  2. rsync同步时,删除目标目录比源目录多余文件的方法(--delete)

    在日常运维工作中,我们经常用到rsync这个同步神器.有时在同步两个目录时,会要求删除目标目录中比源目录多出的文件,这种情况下,就可用到rsync的--delete参数来实现这个需求了. 实例说明:在 ...

  3. ZooKeeper 典型的应用场景——及编程实现

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  4. HTML5-indexedDB使用常见错误总结

    indexedDB使用过程中常常会出现以下错误: Failed to execute ‘createObjectStore’ on ‘IDBDatabase’: The database is not ...

  5. 理解Vue 2.5的Diff算法

    DOM"天生就慢",所以前端各大框架都提供了对DOM操作进行优化的办法,Angular中的是脏值检查,React首先提出了Virtual Dom,Vue2.0也加入了Virtual ...

  6. java感想

    Java学起来很有趣,通过学习Java可以提高自己的逻辑能力.在学习Java期间我们做了一些程序,我们班的同学也都积极准备,完成的还不错!在做程序时,我遇到了一些难题,有时也会出现错误,时间长了弄得我 ...

  7. 面向对象课程 - 寒假第四次作业 - C++计算器项目计算部分

    C++计算器项目计算部分 零.项目源文件地址 地址:Calculator 2.0 一.项目信息相关 项 目 : Calculator 版 本 : 2 . 0 日 期 : 2016 . 4 . 14 实 ...

  8. java 值传递 数组传递

    在java中,不允许程序员选择值传递还是地址传递各个参数,基本类型总是按值传递.对于对象来说,是将对象的引用也就是副本传递给了方法,在方法中只有对对象进行修改才能影响该对象的值,操作对象的引用时是无法 ...

  9. Ajax的注意事项

    case 1: 无论是使用原生的JavaScript,还是JQuery,通过Ajax请求后端程序数据,返回的数据默认是字符串,字符串,字符串,重要的事情说三遍!!! case 2: 不要尝试直接将返回 ...

  10. jeecg 主-附表生成代码例子

    jeecg 主-附表生成代码例子 - CSDN博客https://blog.csdn.net/u010411264/article/details/51243277 JEECG Online Codi ...