C. Oh Those Palindromes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, “abcba”, “a”, and “abba” are palindromes, while “abab” and “xy” are not.
A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, “abc”, “ab”, and “c” are substrings of the string “abc”, while “ac” and “d” are not.
Let’s define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string “aaa” is
6
because all its substrings are palindromes, and the palindromic count of the string “abc” is
3
because only its substrings of length
1
are palindromes.
You are given a string
s
. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count.
Input
The first line contains an integer
n
(
1
≤
n
≤
100
000
) — the length of string
s
.
The second line contains string
s
that consists of exactly
n
lowercase characters of Latin alphabet.
Output
Print string
t
, which consists of the same set of characters (and each characters appears exactly the same number of times) as string
s
. Moreover,
t
should have the maximum possible value of palindromic count among all such strings strings.
If there are multiple such strings, print any of them.
做法是直接按照字典序排序就ok,这样一定是最多的。
#include <bits/stdc++.h>
#include <bitset>
#define ll long long
#define INF 0x3f3f3f3f
#define pr pair<int,int>
#define fi first
#define next fuck
#define se second
using namespace std;
int cnt[300];
int main()
{
string s;
int n;
cin>>n>>s;
for(int i = 0;i<n;i++) cnt[s[i]]++;
vector<pr> o,e;
for(int i = 'a';i<='z';i++)
{
for(int j = 0;j<cnt[i];j++) cout<<(char)(i);
}
cout<<endl;
return 0;
}