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. python-深浅copy-18

    # 赋值运算l1 = [1,2,3]l2 = l1l1.append('a')print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3, 'a'] #copyl1 = [1,2,3 ...

  2. JDK+JAVA+maven+IDEA

    JDK+JAVA https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html maven+IDEA http://blog.csdn ...

  3. hashContext

    java.lnag.Object中对hashCode的约定: 1. 在一个应用程序执行期间,如果一个对象的equals方法做比较所用到的信息没有被修改的话,则对该对象调用hashCode方法多次,它必 ...

  4. Word frequency program ver.1

    先吐槽一下自己 做这个作业的时候完全没有提前考虑好时间 导致要提交前一天晚上通宵写代码 而且还满满的bug TAT 缓一口气 程序还在继续写 先把有点累 想着先把博客写了 PART I 预计时间: 语 ...

  5. Some questions after Reading 《移山之道》

    很少见到用故事的形式来写技术书籍的,这是我看到的第一本,书写得比较有趣,看了之后也是有一定的收获.  作者在此书中旁征博引,引用的东西虽不能一个一个查询是否正确,但是每次读到时候,感觉一种现代的软件工 ...

  6. Linux内核分析(第八周)

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与其时机分析 分类: 第一种分类 I/O-bound:频繁的进行I/O:会花很多时间等待I/O操作完成 CPU ...

  7. sprint最后冲刺-out to out

    摘要:团队合作.实现四则APP,上传代码到github. 1.之前我们队一直无法把代码上传到github.直到今天.找到了一种可以协助代码上github的软件msysgit. 经过:(一行行看) 我们 ...

  8. 战神答题APP 无敌结束版

    APP发布了哦~~     多多捧场~ http://anzhuoyuan.com/app/info/appid/242381.html 还有github https://github.com/784 ...

  9. Ionic package Error: Registry returned 404 for GET on

    Ionic package Error: Registry returned 404 for GET on https://registry.npmjs.org/org.apache.cordova. ...

  10. 使用TensorFlow实现分类

    这一节使用TF搭建一个简单的神经网络用于分类任务,首先把需要的包引入,另外为了防止在多次运行中一些图中的tensor在内存中影响实验,采取重置操作: import tensorflow as tf i ...