cf516div1A Oh Those Palindromes 结论+思维

版权声明:若转载请附上原博客链接,谢谢! https://blog.csdn.net/Link_Ray/article/details/83118133

题意

给一个长度为n的字符串,问如何重组使得字符串中回文的子串数目最多。例如aaa的回文子串个数是a,a,a,aa,aa,aaa,一共6个。

题解

一个出现x次的字符c,能构成的回文子串个数是 x ( x + 1 ) 2 \frac{x*(x+1)}{2} , 这里有个结论是,将相同的字符连在一起组成的字符串中包含的回文子串个数是最多的。所以sort一遍就好。

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	string s;
	cin >> n >> s;
	sort(s.begin(), s.end());
	cout << s << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Link_Ray/article/details/83118133