美文网首页PAT
B1077 Kuchiguse (字符串最长公共后缀)

B1077 Kuchiguse (字符串最长公共后缀)

作者: Tsukinousag | 来源:发表于2020-01-20 23:38 被阅读0次

1077 Kuchiguse (20分)

都反转一下就好处理了。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
const int MAX=100005;
const int INF=0x3f3f3f3f;
const int mod=1000000007;
string s;
vector<string>vt;
bool cmp(string a,string b)
{
    return a.size()>b.size();
}
int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++)
    {
        getline(cin,s);
        reverse(s.begin(),s.end());
        vt.push_back(s);
    }
    sort(vt.begin(),vt.end(),cmp);
    string temp;
    int flag=0,i;
    for(i=0;i<vt[0].size();i++)
    {
        for(int j=0;j<n;j++)
        {
            if(vt[j][i]!=vt[0][i])
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
            break;
        temp+=vt[0][i];
    }
    if(i==0&&flag==1)
    {
        cout<<"nai"<<endl;
    }
    else
    {
    reverse(temp.begin(),temp.end());
    cout<<temp<<endl;
    }
    return 0;
}

相关文章

  • B1077 Kuchiguse (字符串最长公共后缀)

    1077 Kuchiguse (20分) 都反转一下就好处理了。

  • 最长公共子串

    问题: 找出最长、连续的子字符串 思路: 遍历X、Y的所有子字符串,找出最长公共后缀,则最长公共后缀的长度就是最长...

  • 大学被虐了很久的kmp算法

    关键在于实现最长公共前后缀table。一个字符串的最长公共前后缀举例:比如"abcdabc"的最长公共前后缀为“a...

  • 字符串算法

    最长公共前缀 最长回文串 最长回文子序列 最长公共子串 反转单词顺序列 反转字符串 字符串转数字 IP-int互转

  • 【python】求两个字符串的公共字串?

    题目:找出两个字符串的最长公共字串,例如字符串“abccade”与字符串“dgcadde”的最长公共子串为“cad...

  • 5,最长公共前缀/数组与字符串

    最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例 1:...

  • Swift 最长公共前缀 - LeetCode

    题目: 最长公共前缀 描述: 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""...

  • leetcode探索之旅(14)

    最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。 示例 1: ...

  • Leetcode 14 最长公共前缀

    最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例...

  • LeetCodeSwift 14.Longest Common

    题目 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例...

网友评论

    本文标题:B1077 Kuchiguse (字符串最长公共后缀)

    本文链接:https://www.haomeiwen.com/subject/aemszctx.html