码迷,mamicode.com
首页 > 其他好文 > 详细

1077. Kuchiguse (20)

时间:2015-12-06 12:53:57      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

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. #include <string>
    2. #include <vector>
    3. #include <iostream>
    4. #include <stdio.h>
    5. #pragma warning(disable:4996)
    6. using namespace std;
    7. vector<char> kuchi;
    8. vector<string> s;
    9. int main(void) {
    10. int n;
    11. cin >> n;
    12. char ctemp;
    13. scanf("%c", &ctemp);
    14. for (int i = 0; i < n; i++) {
    15. string temp;
    16. while (true)
    17. {
    18. scanf("%c", &ctemp);
    19. if (ctemp != ‘\n‘)
    20. temp += ctemp;
    21. else
    22. break;
    23. }
    24. s.push_back(temp);
    25. }
    26. int p = 1;
    27. char c;
    28. bool flag = true;
    29. if (s[0].length() == 0) {
    30. cout << "nai";
    31. return 0;
    32. }
    33. while (true)
    34. {
    35. c = s[0][s[0].length() - p];
    36. for (int i = 1; i < n; i++) {
    37. if (s[i].length() < p) {
    38. flag = false;
    39. break;
    40. }
    41. if (s[i][s[i].length() - p] != c) {
    42. flag = false;
    43. break;
    44. }
    45. }
    46. if (flag == false)
    47. break;
    48. kuchi.push_back(c);
    49. p++;
    50. }
    51. if (kuchi.size() == 0) {
    52. cout << "nai" << endl;
    53. }
    54. else {
    55. string kuchigusei;
    56. for (int i = kuchi.size() - 1; i >= 0; i--)
    57. kuchigusei += kuchi[i];
    58. cout << kuchigusei;
    59. }
    60. return 0;
    61. }





    1077. Kuchiguse (20)

    标签:

    原文地址:http://www.cnblogs.com/zzandliz/p/5023248.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!