首页 > 其他 > 详细

PAT A1077 Kuchiguse

时间:2019-08-26 19:52:36      阅读:86      评论:0      收藏:0      [点我收藏+]

PAT A1077 Kuchiguse

题目描述:

  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

参考代码:

 

 1 /****************************************************
 2 PAT A1077 Kuchiguse
 3 ****************************************************/
 4 #include <iostream>
 5 #include <vector>
 6 #include <string>
 7 
 8 using namespace std;
 9 
10 int main() {
11     int sentenceCnt = 0;
12     vector<string> Sentences;
13 
14     cin >> sentenceCnt;
15 
16     string temp;
17     getchar();                 //接收换行符
18     int minSenLen = 300;       //记录最短的句子的长度,句子最长为256因此初始值设置为300
19     for (int i = 0; i < sentenceCnt; ++i) {
20         getline(cin, temp);
21         Sentences.push_back(temp);
22         
23         minSenLen = minSenLen < temp.size() ? minSenLen : temp.size();
24     }
25 
26     string ans;             //ans长度小于最长度最短的那句话
27     for (int i = 0; i < minSenLen; ++i) {
28         char c = Sentences[0][Sentences[0].size() - 1 - i];
29         for (int j = 0; j < sentenceCnt; ++j) {
30             if (c != Sentences[j][Sentences[j].size() - 1 - i]) {  //出现了不同句子到数相同的位置的字符不相同的情况
31                 cout << (ans.size() == 0 ? "nai" : ans);
32                 return 0;
33             }
34         }
35         ans.insert(ans.begin(), c);
36     }
37 
38     cout << ans;             //如果ans等于长度最小的那句话
39 
40     return 0;
41 }

注意事项:

  1:注意最后寻找“Kuchiguse”的时候如果它的长度小于所有句子的长度,那么上述代码中最后一个for循环就可以处理了。但是如果“Kuchiguse”的长度等于所最短句子的长度for循环是不会处理的因此在循环之后还要添加输出ans的语句,这也是题目中最后一个测试点测试的地方。

PAT A1077 Kuchiguse

原文:https://www.cnblogs.com/mrdragon/p/11414396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!