只要将每一种字母放一块输出就行了。
证明1:比如 1 2 3 4 5 6,那么这个序列对答案的贡献分别是1和5,2和4 ,3和6……如果重新排列成x x x x o o,会发现对 x x o x x o 答案的贡献不变,所以得证。
证明2:字母ai有xi个,那么对答案的最大贡献为xi * (xi - 1) / 2,重排后能达到理论上界,所以为最优解。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 using namespace std;12 #define enter puts("") 13 #define space putchar(' ')14 #define Mem(a, x) memset(a, x, sizeof(a))15 #define rg register16 typedef long long ll;17 typedef double db;18 const int INF = 0x3f3f3f3f;19 const db eps = 1e-8;20 const int maxn = 1e5 + 5;21 inline ll read()22 {23 ll ans = 0;24 char ch = getchar(), last = ' ';25 while(!isdigit(ch)) last = ch, ch = getchar();26 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();27 if(last == '-') ans = -ans;28 return ans;29 }30 inline void write(ll x)31 {32 if(x < 0) x = -x, putchar('-');33 if(x >= 10) write(x / 10);34 putchar(x % 10 + '0');35 }36 37 int n, a[30];38 char c[maxn];39 40 int main()41 {42 n = read(); scanf("%s", c);43 for(int i = 0; i < n; ++i) a[c[i] - 'a']++;44 for(int i = 0; i <= 26; ++i)45 if(a[i]) while(a[i]--) putchar(i + 'a');46 enter;47 return 0;48 }