div2 620 B

B

Tag

字符串,贪心,思维,set,暴力


回文串的最终形式一定是

\(S_1+S_2+S_3+...+S_{mid}+...+S_{n-2}+S_{n-1}+S_n\)

我们只需要统计出有多少对 回文串,然后把他们分别放到两边,最后在把自己跟自己回文的串放到中间

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
string s[N];
set<string> dict;
int main() {
    int n,m;
    cin >> n >> m;
    for(int i = 0;i < n; ++i) {
        cin >> s[i];
        dict.insert(s[i]);
    }
    vector<string> left,right;
    string mid ;
    for(int i = 0;i < n; ++i) {
        string t = s[i];
        reverse(t.begin(),t.end());
        if(t == s[i]) {
            mid = t;
        }
        else if(dict.find(t) != dict.end()) {
            left.push_back(s[i]);
            right.push_back(t);
            dict.erase(s[i]);
            dict.erase(t);
        }
    }
    cout << left.size() * m * 2 + mid.size() << endl;
    for(auto x : left) cout << x;
    cout << mid;
    reverse(right.begin(),right.end());
    for(auto x : right) cout << x;
    cout << endl;
    return 0;
}
posted @ 2020-02-22 14:06  南风--  阅读(79)  评论(0编辑  收藏  举报