博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT---A1077. Kuchiguse (20)
阅读量:4005 次
发布时间:2019-05-24

本文共 2238 字,大约阅读时间需要 7 分钟。

题目要求:

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

解题思路: 由于判断共同后缀,则将字符串反转再判断可以省很多事。反转后,判断第0位是否为共同后缀(必须所有的字符串的这一位都要相同,可以以第一个字符串作为基准,判断其他字符串的第0位是否与其相同 ),然后再判断第一位,第二位。。。。。。只要有一位不是所有字符串都相同,则判断结束

参考代码:

#include 
#include
#include
using namespace std;int main(){ int minLen = 256; //假设一个最大值(随意定)。 int N; cin >>N; int ans = 0; char s[102][256]; //使用二维数组来存储字符串 getchar(); //因为随后要输入的是字符类型,所以要使用getchar先将换行符取走 for(int i=0;i
=0;i--) cout <

此外,本人还写出了一种实现方法,但有诸多测试点错误,在此贴出,望各位大神能看到后给予宝贵的解答

#include 
#include
using namespace std;//反转字符串void reverse_fun(string *a){ for(int i = 0;i<(*a).length()/2;i++) { char b; b = (*a)[i]; (*a)[i] = (*a)[(*a).length()-1-i]; (*a)[(*a).length()-1-i] = b; }}int main(){ int minLen = 259; int N; cin >>N; int flag = 0; //flag为0表明没有共同后缀 char common[100]; string s[102]; //使用字符串数组来存储输入 getchar(); //获取换行符 for(int i=0;i
=0;i--) cout <

转载地址:http://hbzfi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
面试---刷牛客算法题
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>
SVN客户端命令详解
查看>>
Android/Linux 内存监视
查看>>
Linux系统信息查看
查看>>