zoukankan      html  css  js  c++  java
  • D1. Prefix-Suffix Palindrome (Easy version)

    This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

    You are given a string ss, consisting of lowercase English letters. Find the longest string, tt, which satisfies the following conditions:

    • The length of tt does not exceed the length of ss.
    • tt is a palindrome.
    • There exists two strings aa and bb (possibly empty), such that t=a+bt=a+b ( "++" represents concatenation), and aa is prefix of ss while bb is suffix of ss.
    Input

    The input consists of multiple test cases. The first line contains a single integer tt (1t10001≤t≤1000), the number of test cases. The next tt lines each describe a test case.

    Each test case is a non-empty string ss, consisting of lowercase English letters.

    It is guaranteed that the sum of lengths of strings over all test cases does not exceed 50005000.

    Output

    For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

    Example
    input
    Copy
    5
    a
    abcdfdcecba
    abbaxyzyx
    codeforces
    acbba
    
    output
    Copy
    a
    abcdfdcba
    xyzyx
    c
    abba
    
    Note

    In the first test, the string s=s="a" satisfies all conditions.

    In the second test, the string "abcdfdcba" satisfies all conditions, because:

    • Its length is 99, which does not exceed the length of the string ss, which equals 1111.
    • It is a palindrome.
    • "abcdfdcba" == "abcdfdc" ++ "ba", and "abcdfdc" is a prefix of ss while "ba" is a suffix of ss.

    It can be proven that there does not exist a longer string which satisfies the conditions.

    In the fourth test, the string "c" is correct, because "c" == "c" ++ "" and aa or bb can be empty. The other possible solution for this test is "s".

    每次处理字符串的时候最容易犯的错就是下标搞不清楚,如果把具体的数字带进去,该减一还是该加一更清楚一点。

    这道题目我把只有一个的情况和本身就是回文串的情况特判了一下。

    #include <bits/stdc++.h>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const int mod = 998244353;
    const int N = 1e5+5;
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m%n);
    }
    ll lcm(ll m, ll n)
    {
        return m*n / gcd(m, n);
    }
    bool cmp(PII a, PII b)
    {
        return a.second<b.second;
    }
    bool ispalin(string s)
    {
        int len = s.size();
        for(int i=0;i<len/2;i++)
        {
            if(s[i]!=s[len-1-i])
                return false;
        }
        return true;
    }
    int main() {
        int T;
        cin>>T;
        while(T--)
        {
            string s;
            cin>>s;
            int len=s.size();
            int pos=len/2;
            if(ispalin(s))
            {
                cout<<s<<endl;
                continue;
            }
            for(int i=0;i<s.size();i++)
            {
                if(s[i]!=s[len-i-1])
                {
                    pos=i;
                    break;
                }
            }
            string pre , suf;
            for(int i=pos;i<len-pos-1;i++)
            {
                if(ispalin(s.substr(pos,i-pos+1)))
                    pre=s.substr(pos,i-pos+1);
            }
            for(int i=len - pos-1;i>pos;i--)
            {
                if(ispalin(s.substr(i,len-pos-i)))
                    suf=s.substr(i,len-pos-i);
            }
    
            string mid=pre.size()>suf.size()?pre:suf;
            string res;
            if(2*(pos+1)<=len)
                res=s.substr(0,pos)+mid+s.substr(len-pos,pos);
            else
                res=s.substr(0,pos+1);
            cout<<res<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    JAVA_Collection容器
    ArrayList实现分组功能
    scrapy 安装出错 [err2] no such file or directory: 'README.rst'【已解决】
    python spyder 今天突然打不开了【已解决】
    SVN使用教程总结
    MVC框架浅析(基于PHP)
    Web性能优化方案
    野生程序员的故事
    js控制页面跳转,清缓存,强制刷新页面
    js中json处理总结之JSON.parse
  • 原文地址:https://www.cnblogs.com/dealer/p/12920360.html
Copyright © 2011-2022 走看看