A1077. Kuchiguse (20)

来源:互联网 发布:淘宝货源一件代发网 编辑:程序博客网 时间:2024/05/16 06:33

1.题目描述

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

2.解题过程

本题有几点需要注意:
1) 输入n后几个用getchar()读掉换行符,读入一行字符的时候用gets(),因为scanf()会将空格看做分割符。
2)一开始没有用字符串倒置函数,但是后来发现字符串长度是不固定的,所以从最后一位开始判断会比较麻烦,用倒置函数比较清晰,也容易理解。
3)第一次提交后有一个测试用例没有通过,原因是在遇到有字符不同后要用break跳出外层的for循环,停止后面的比较,否则可能有一段字符不相同后,又恰好出现所有字符串相同位置的字符都相同的情况。
错误代码部分单独列出比较,如下:
原代码:

for(int i=0;i<minLen;i++){        char t = a[0][i];        flag = true;        for(int j=1;j<n;j++){            if(a[j][i]!=t){                flag = false;                break;            }        }        if(flag)            count++;    }

修改后:

for(int i=0;i<minLen;i++){        char t = a[0][i];        flag = true;        for(int j=1;j<n;j++){            if(a[j][i]!=t){                flag = false;                break;            }        }        if(flag)            count++;        else            break;    }

完整代码:

    /*A1077*/#include<cstdio>#include<cstring>#define NUM 101#define LEN 260//字符串反转函数void reverse(char a[]){    int len = strlen(a);    char t;    for(int i=0;i<len/2;i++){        t = a[i];        a[i] = a[len-i-1];        a[len-i-1] = t;    }}int main(){    int n,count = 0,minLen = LEN;    char a[NUM][LEN];    bool flag = true;    scanf("%d",&n);    getchar();    for(int i=0;i<n;i++){        gets(a[i]);        if(strlen(a[i])<minLen){            minLen = strlen(a[i]);        }        reverse(a[i]);    }    for(int i=0;i<minLen;i++){        char t = a[0][i];        flag = true;        for(int j=1;j<n;j++){            if(a[j][i]!=t){                flag = false;                break;            }        }        if(flag)            count++;        else            break;    }    if(count==0)        printf("nai");    else{        for(int i=count-1;i>=0;i--){            printf("%c",a[0][i]);        }    }    return 0;}

3.总结

遇到字符和字符串连续输入时,要记得考虑不同读入方法的分隔符,选择合适的方法读入,同时记得读掉多余的空格,换行符等。
另外,写程序时要更仔细一点,少一些这题出现的这种考虑不全的错误。

原创粉丝点击