CF1063A 【Oh Those Palindromes】
考虑在一个部分串中加入字符使得最终构造的串回文子串最多的方案
考虑简单情况,对于只含一种元素的串,我们要插入其他元素
记原有元素为$a$,新加元素为$b$
考虑$b$的最优插入位置
原串$aaaa...aa$,插入$b$
设$b$在串中的插入位置为$pos$,插入后,原本的回文串$[pos-i,pos+j](i!=j)$会因此不匹配
所以这样不会使得原串匹配结果变多
所以我们要让各个元素独立才是最优方案
#include<iostream> #include<cstdio> using namespace std; string s; char ans[100010]; int l,cnt[100010],cnts; int main() { cin>>l>>s; for(int i=0;i<l;i++) cnt[s[i]-'a']++; for(int i=0;i<26;i++) for(int j=1;j<=cnt[i];j++) ans[cnts++]=i+'a'; printf("%s\n",ans); return 0; }