A1077 Kuchiguse Suffix-后缀
Posted pennyxia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A1077 Kuchiguse Suffix-后缀相关的知识,希望对你有一定的参考价值。
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). 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
思路:
• 输入n个字符串,存到字符串数组中,将所有字符串进行reverse,然后从第一个元素开始查找,直到n个字符串对应位置不一样时停止;
• 将最后结果截取存到str中,翻转后输出。
问题:
问题1:最后一行无法输入
问题2:无法正常输出“nai”
解决:
解决1:getline()之前需要用getchar()吸收cin>>n之后的回车
解决2:if (i == 1)cout << "nai";改成if (i == 0)cout << "nai";
因为for循环的运行是这样的:
我们把其命名为表达式1、2、3。for执行时首先执行表达式1,然后执行表达式2,如果循环成立,在循环结束后,下一个循环前执行表达式3,然后再执行表达式2进行判断。
所以,在该题中条件不成立时在循环中break,即跳出循环,所以不会再执行i++;如果是正常结束循环,会执行i++,然后判断表达式2是否成立判断是否需要结束循环。
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 int main() { 6 int n,minlen=256; 7 string s[105]; 8 cin >> n; 9 getchar(); 10 //scanf_s("%d", &n); 11 for (int i = 0; i < n; i++) { 12 getline(cin, s[i]); 13 reverse(s[i].begin(), s[i].end()); 14 if (s[i].length() < minlen) 15 minlen = s[i].length(); 16 } 17 int i=0,tag = 0; 18 for (i = 0; i < minlen; i++) { 19 for (int j = 1; j < n; j++) { 20 if (s[j][i] != s[0][i]) { 21 tag = 1; 22 break; 23 } 24 } 25 if (tag == 1)break; 26 } 27 if (i == 0)cout << "nai"; 28 else { 29 string str= s[0].substr(0, i); 30 reverse(str.begin(), str.end()); 31 cout << str; 32 } 33 return 0; 34 }
以上是关于A1077 Kuchiguse Suffix-后缀的主要内容,如果未能解决你的问题,请参考以下文章
PAT甲题题解-1077. Kuchiguse (20)-找相同后缀
1077 Kuchiguse (20point(s)) Easy only once
Pandas批量删除dataframe列名中的后缀实战:使用rstrip函数批量删除列名中的后缀(suffix)使用replace函数批量删除列名中的后缀(suffix)