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<cstring>
using namespace std;
int main()
{int N;int ans=0;scanf("%d",&N);int minlen = 256;char a[101][300];getchar();for(int i=0;i<N;i++){fgets(a[i],256,stdin);int len = strlen(a[i]);if(minlen>len) minlen =len;for(int j=0;j<minlen/2;j++)//reverse{char temp;temp = a[i][j];a[i][j]=a[i][-j+len-1];a[i][-j+len-1]=temp;}}for(int i=1;i<minlen;i++){bool flag=true;char c = a[0][i];for(int j = 1;j<N;j++){if(c!=a[j][i]){flag = false;break;}}if(flag) ans++;else break;}if(ans){for(int i=ans-1;i>=1;i--){printf("%c",a[0][i]);}}else{printf("nai");}return 0;}

感觉有点混乱

#include <iostream>
#include <algorithm>
using namespace std;int main() {int n;scanf("%d\n", &n);string ans;for(int i = 0; i < n; i++) {string s;getline(cin, s);int lens = s.length();reverse(s.begin(), s.end());if(i == 0) {ans = s;continue;} else {int lenans = ans.length();int minlen = min(lens, lenans);for(int j = 0; j < minlen; j++) {if(ans[j] != s[j]) {ans = ans.substr(0, j);break;}}  }}reverse(ans.begin(), ans.end());if (ans.length() == 0)ans = "nai";
cout << ans;
return 0;
}

以上的解法更加简便

使用到的新鲜函数是:getline, reverse, min

1. getline

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

字符串的输入方式之一,特点是遇到空格不停止输入

2. reverse

反转范围中元素的顺序[first,last)

3. min

输出两数中较小的一个,同理max是较大那个

1077.Kuchiguse(20分)相关推荐

  1. 1077 Kuchiguse (20分)

    并不是很复杂的判断尾部相同公共部分. 注意要保证最长相同子串要小于等于最短字符串长度: 最后一个测试点,也就是测试点4就是这种情况,其中一个字符串刚好等于公共字符串. 附本人AC代码: #includ ...

  2. 1077.Kuchiguse (20)

    1077.Kuchiguse (20) pat-al-1077 2017-01-27 本题无坑 方法:把字符串一个一个读进来,每个都逆序一下,并且在这个过程中记录一下最短的字符串的长度:然后开始对每个 ...

  3. A-1077 Kuchiguse (20 分)

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

  4. A1077 Kuchiguse (20分)【C语言】

    A1077 Kuchiguse (20分)[C语言] 原题链接 要比较每个字符串的最后几个字母是否相同, 把字符串倒序处理会简单一点,因此先将字符串倒过来,然后以所有字符串中最短的为基准,挨个扫描是否 ...

  5. 【1077】Kuchiguse (20 分)

    #include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #in ...

  6. 1077. Kuchiguse (20)-PAT甲级真题

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

  7. 1077. Kuchiguse (20)

    题目如下: The Japanese language is notorious for its sentence ending particles. Personal preference of s ...

  8. 【PAT甲级】11077 Kuchiguse (20 分) Java

    题目 Java题解(最后一个测试用例没通过) import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  9. PAT甲题题解-1077. Kuchiguse (20)-找相同后缀

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

最新文章

  1. 以后所有内容均以摘要方式发布
  2. 奇妙的安全旅行之DES算法(二)
  3. Exchange邮箱数据库事务日志引起磁盘暴涨
  4. 《笑傲网湖》第一回 从HUB和LANSWITCH开始
  5. ubuntu下配置安装PyQt4
  6. 使用整数除法获得商c语言,C语言练习题答案
  7. 首席CEO乔布斯他是如何拉住了苹果快破产的边缘?他的运营方法是什么呢?
  8. 银行理财产品的十个坑
  9. 走近篮球运动·体育项目
  10. 播放视频时过滤播霸浏览器
  11. UVALive 6657 GCD XOR 异或,因子筛法
  12. 最近比较火的palywright了解一下?
  13. CentOs7 安装绿色版Nginx并配置开机启动
  14. 计算机二级c语言模拟上机,计算机二级C语言上机模拟题
  15. 集合转换成数组的两种方法---toArray()和toArray(T[] a)
  16. CCF-CSP Python Cheat Sheet
  17. [转载]python库收集贴
  18. vue项目通过url链接引入其他系统页面
  19. eMule中的kad相关代码梳理
  20. Ai实现FPS游戏自动瞄准 yolov5fps自瞄

热门文章

  1. AlphaGo是如何炼成的:解读论文 Mastering the game of Go with deep neural networks and tree search
  2. Adobe Photoshop CS3绿色精简版(26M)
  3. dao层动态sql利器!数据持久化jpa插件spring-data-jpa-extra?Fenix!
  4. java设计模式中不属于创建型模式_Java设计模式(5)——创建型模式之建造者模式(Builder)...
  5. Bancor的原理及智能合约实现
  6. 关于Hessian矩阵的图像增强
  7. 中国房价长期暴跌不可避免!
  8. 毕业设计 - 基于51单片机的电子时钟设计与实现
  9. Office文档图标变为未知图标,office2106安装完,打开软件是提示错误
  10. 3G手机视频对讲开发包