zoukankan      html  css  js  c++  java
  • 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 }

     

    作者:PennyXia
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Git忽略规则.gitignore梳理
    计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解
    Storm:最火的流式处理框架
    vue-自定义组件传
    HashMap与HashTable的区别
    iOS下JS与OC互相调用(二)--WKWebView 拦截URL
    iOS下JS与OC互相调用(一)--UIWebView 拦截URL
    iOS load和initialize的区别
    iOS下nil 、NULL、 Nil 、NSNull的区别
    iOS正确解决隐藏导航栏后push和pop或dismiss和present闪黑问题
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12296206.html
Copyright © 2011-2022 走看看