PAT (Advanced Level) 1077. Kuchiguse (20)

最长公共后缀。暴力。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

char s[200][300];
int len[200];
int n;

int main()
{
    memset(s,0,sizeof s);
    scanf("%d",&n);
    getchar();
    for(int i=1; i<=n; i++) gets(s[i]);
    for(int i=1; i<=n; i++) len[i]=strlen(s[i]);

    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<len[i]/2; j++)
            swap(s[i][j],s[i][len[i]-j-1]);
    }

    bool fail=0;
    for(int j=0;s[1][j]; j++)
    {
        for(int i=2; i<=n; i++)
            if(s[i][j]!=s[i-1][j]) fail=1;
        if(fail==1)
        {
            if(j==0) printf("nai\n");
            else
            {
                for(int k=j-1; k>=0; k--)
                    printf("%c",s[1][k]);
                printf("\n");
            }
            break;
        }
    }

    if(fail==0)
    {
        for(int k=len[1]-1; k>=0; k--)
            printf("%c",s[1][k]);
        printf("\n");
    }
    return 0;
}
时间: 2024-12-29 23:15:30

PAT (Advanced Level) 1077. Kuchiguse (20)的相关文章

PAT (Advanced Level) 1008. Elevator (20)

简单模拟. 注意a[i]==a[i-1]的情况. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; int n,a[maxn]; int main() { scanf("%d&

PAT (Advanced Level) 1061. Dating (20)

简单模拟. #include<stdio.h> #include<string.h> char s1[70],s2[70],s3[70],s4[70]; char f[7][5]={"MON ", "TUE ","WED ","THU ","FRI ","SAT ","SUN "}; int a,b,c,flag; int main() { s

【PAT甲级】1077 Kuchiguse (20 分)(cin.ignore()吃掉输入n以后的回车接着用getine(cin,s[i])输入N行字符串)

题意: 输入一个正整数N(<=100),接着输入N行字符串.输出N行字符串的最长公共后缀,否则输出nai. 代码: #include<bits/stdc++.h>using namespace std;string s[107];int length[107];char ans[307];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n; cin.igno

1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 prefere

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names