PAT 甲级 1077 Kuchiguse (20 分)

深碍√TFBOYSˉ_ 2024-02-19 21:04 146阅读 0赞

1077 Kuchiguse (20 分)

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:

  1. 3
  2. Itai nyan~
  3. Ninjin wa iyadanyan~
  4. uhhh nyan~

Sample Output 1:

  1. nyan~

Sample Input 2:

  1. 3
  2. Itai!
  3. Ninjinnwaiyada T_T
  4. T_T

Sample Output 2:

  1. nai
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8. string com="",temp;
  9. int i,j,n,c,t;
  10. cin>>n;
  11. getchar();
  12. getline(cin,com);
  13. for(i=1;i<n;i++)
  14. {
  15. getline(cin,temp);
  16. c=com.length()-1;
  17. t=temp.length()-1;
  18. while(com[c]==temp[t])
  19. {
  20. c--;
  21. t--;
  22. if(c<0||t<0) // 这点我错过
  23. break;
  24. }
  25. if(c==com.length()-1)
  26. {
  27. com="nai";
  28. }
  29. else
  30. {
  31. com=com.substr(c+1);
  32. }
  33. }
  34. cout<<com<<endl;
  35. return 0;
  36. }

发表评论

表情:
评论列表 (有 0 条评论,146人围观)

还没有评论,来说两句吧...

相关阅读