Codeforces 1304B. Longest Palindrome

The data range, violence can be solved, for each string, find their mutually palindromic sequence, or whether it is a palindromic sequence is determined, then the twenty-two palindromic sequence each other are arranged in the head and tail, and only the middle put Up itself can put a string of string is a palindrome, as the title says each string is different

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;

string str[105];
int cache[105], self[105], s[105], chosen[105];

void run_case() {
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) 
        cin >> str[i];
    for(int i = 1; i <= n; ++i) {
        bool flag = true;
        for(int j = 0; j < m/2 && flag; ++j) {
            if(str[i][j] != str[i][m-j-1]) flag = false;
        }
        if(flag) {
            self[i] = 1;
            continue;
        }
        if(cache[i]) continue;
        string now = str[i];
        reverse(now.begin(), now.end());
        for(int j = 1; j <= n; ++j) {
            if(j != i && now == str[j]) {
                cache[i] = j; break;
            }
        }
    }
    vector<int> ans, selfs;
    int top = 0;
    for(int i = 1; i <= n; ++i)
        if(cache[i] && !chosen[i]) {
            ans.push_back(i);
            s[++top] = cache[i];
            chosen[cache[i]] = i;
        } else if(self[i]) selfs.push_back(i);
    if(selfs.size()) ans.push_back(selfs[0]);
    while(top) {
        ans.push_back(s[top--]);
    }
    cout << ans.size() * m << "\n";
    for(auto i : ans) 
        cout << str[i];
}
 
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    //cout.setf(ios_base::showpoint);cout.precision(10);
    //int t; cin >> t;
    //while(t--)
    run_case();
    cout.flush();
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/GRedComeT/p/12316571.html