Codeforces Round #620 (Div. 2) Longest Palindrome

Report problem solving:

Ideas: Find palindrome strings and strings of it, find themselves palindrome string.

The simulation process:

4 2
oo
ox
xo
xx

And it constitutes a palindrome string string: ox xo

Is itself palindromic sequence: oo xx (requires only one, because the strings are not equal between)

The answer: oxooxo length is 6.

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 100;
string ss[N];
bool check(string x, string y){
    ll len = x.size();
    for(ll i=0; i<len; ++i){
        if(x[i] != y[len-i-1])return 0;
    }
    return 1;
}
int main(){
    ll n, m;
    while(~scanf("%lld%lld", &n, &m)){
        for(ll i=0; i<n; ++i)
            cin >> ss[i];
        string x, invX, temp;
        for(ll i=0; i<n; ++i){
            for(ll j=i; j<n; ++j){
                if(check(ss[i], ss[j]) && i == j)temp = ss[i];
                if(check(ss[i], ss[j]) && i != j)x += ss[i];
            }
        }
        ll len = x.size();
        for(ll i=len-1; i>=0; --i)
            invX += x[i];
        x = x + temp + invX;
        cout << x.size() << endl << x << endl;
    }
    return 0;
}

 

Published 208 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/jun_____/article/details/104424296