PAT-A1077. Kuchiguse (20)(模拟)

来源:互联网 发布:怎样抢到淘宝秒杀产品 编辑:程序博客网 时间:2024/05/16 06:25

最长公共后缀,逆置处理。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>using namespace std;#define MAX 256+10char words[100][MAX];void revers(char str[]){    for (int i = 0; i < strlen(str)/2; i++)    {        char t;        t = str[i];        str[i] = str[strlen(str)-1-i];        str[strlen(str)-1-i] = t;    }    return ;}int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    int N = 0, minlen = MAX, cnt = 0;    char temp[MAX], suffix[MAX];    cin >> N;    getchar();    for (int i = 0; i < N; i++)    {        gets(temp);        revers(temp);        strcpy(words[i], temp);        if (strlen(temp) < minlen)            minlen = strlen(temp);    }    for (int i = 0; i < minlen; i++)    {        int f = 0;        for (int j = 1; j < N; j++)        {            if (words[j][i] != words[0][i])            {                f = 1;                break;            }        }        if (1 == f)            break;        suffix[cnt++] = words[1][i];    }    suffix[cnt] = '\0';    revers(suffix);    if (0 == cnt)        cout << "nai";    else        cout << suffix;    return 0;}


0 0