PAT甲题题解-1077. Kuchiguse (20)-找相同后缀

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
找最长相同后缀
*/
const int maxn=105;
char str[maxn][300];
char ans[300];
int len[maxn];
int main()
{
    int n;
    int cnt=0;
    int minlen=300;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++){
        gets(str[i]);
        len[i]=strlen(str[i]);
        minlen=min(minlen,len[i]);
    }
    bool flag;
    for(int i=1;i<=minlen;i++){
        flag=true;
        for(int j=1;j<n;j++){
            if(str[j][len[j]-i]!=str[j-1][len[j-1]-i]){
                flag=false;
                break;
            }
        }
        if(!flag){
            break;
        }
        else{
            ans[cnt++]=str[0][len[0]-i];
        }
    }
    if(cnt==0){
        printf("nai");
    }
    else{
        //if(ans[cnt-1]==‘ ‘)
            //cnt--;
        for(int i=cnt-1;i>=0;i--){
            printf("%c",ans[i]);
        }
    }
    return 0;
}

时间: 2024-08-08 09:40:14

PAT甲题题解-1077. Kuchiguse (20)-找相同后缀的相关文章

PAT甲题题解-1035. Password (20)-水

题意:给n个用户名和密码,把密码中的1改为@,0改为%,l改为L,O改为o. 让你输出需要修改密码的用户名个数,以及对应的用户名和密码,按输入的顺序.如果没有用户需要修改,则输出对应的语句,注意单复数... 没啥好说的,就for一遍密码,把需要改的改下,存入到ans中去. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cm

PAT甲题题解-1008. Elevator (20)-大么个大水题,这也太小瞧我们做题者的智商了

如题... #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> using namespace std; //太水了好吧... int n; int timeup=6; int timedown=4; int main() { scanf("%d",&n); int last=

PAT甲题题解-1036. Boys vs Girls (25)-找最大最小,大水题

题意:给出n个人的姓名.性别.ID.分数,让你找出其中哪个妹纸分数最高.哪个汉子分数最低.以及他们的差如果没有妹纸或者汉子,则对应输出Absent,差用NA代替. 就是for一遍找最大最小值,水题 #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> #include <vector> #defin

PAT甲题题解-1006. Sign In and Sign Out (25)-找最小最大

判断哪个人最早到,哪个人最晚走水,就是找最大值最小值 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string.h> using namespace std; int m; char unlock[20],lock[20]; char start[20]="25:00:00",endtime[20

PAT甲题题解-1108. Finding Average (20)-字符串处理

求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=105; bool islegal(char*str){ int len=strlen(str); int p

PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可以分解2*3*4*6*7*8,最大的连续个数为3,因为存在两个,输出最小的那个即2*3*4. 首先,一个数如果是合数,那么它的因子必定不会超过sqrt(n)或者sqrt(n)+1.如果为质数,那么只可能为自己,因为题目说了不包括1. 我们先将2~sqrt(n)+1中为n的因子存到factor数组中,

PAT甲题题解-1112. Stucked Keyboard (20)-(map应用)

题意:给定一个k,键盘里有些键盘卡住了,按一次会打出k次,要求找出可能的坏键,按发现的顺序输出,并且输出正确的字符串顺序. map<char,int>用来标记一个键是否为坏键,一开始的时候都为0,表明所有的键为坏键. 然后遍历每个字符,统计当前字符连续出现的次数cnt,则只要存在cnt%k!=0,则表明为好键,另其map=1. 最后再for一遍字符串,存储坏键字符串和正确字符串,最后输出即可. #include <iostream> #include <cstdio>

PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验细节处理,其它没啥好说的. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; long long numerato

PAT甲题题解-1011. World Cup Betting (20)-误导人的水题。。。

题目不严谨啊啊啊啊式子算出来结果是37.975样例输出的是37.98我以为是四舍五入的啊啊啊,所以最后输出的是sum+0.005结果告诉我全部错误啊结果直接保留两位小数就可以了啊啊啊啊 水题也不要这么坑人啊啊啊啊 #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int main() { double a[3];