J - 超排序
Description
bLue 在跨年之際獲得了一份小禮物,里面裝了一串只包含大寫字母和小寫字母的字符串,如果你能幫 bLue 把這個字符串按照字典序排序(按 ASCII 碼從小到大排序。大寫字母的 ASCII 碼小于小寫字母的 ASCII 碼),他會獎勵你一個 Accepted。
Input
輸入數(shù)據(jù)有多組(數(shù)據(jù)組數(shù)不超過 50),到 EOF 結(jié)束。
對于每組數(shù)據(jù),輸入一行只包含大寫字母和小寫字母的字符串,且長度不超過 1000000。
Output
對于每組數(shù)據(jù),輸出一行排序后的字符串。
Sample
Input
Output
HNYaaeepprwy AAABBaaabbbcdHint
由于數(shù)據(jù)量較大,不推薦直接使用 cin, cout 輸入輸出。
另外,請確保最終結(jié)果是直接輸出整個字符串,而非使用 printf("%c") 或 putchar() 等函數(shù)一個一個地輸出字符,否則可能導(dǎo)致超時。
#include<bits/stdc++.h>using namespace std;const int N = 1e7 +10; char s[N]; int a[200];int main() {ios::sync_with_stdio(0);while(cin >> s){memset(a, 0, sizeof(a));int k = 0, n = strlen(s);for(int i = 0; i < n; i++){a[(int)s[i]]++;}for(int i = 'A'; i <= 'Z'; i++){for(int j = 0; j < a[i]; j++)//a[i]表示當(dāng)前i字符出現(xiàn)了幾次s[k++] = i;}for(int i = 'a'; i <= 'z'; i++){for(int j = 0; j < a[i]; j++)s[k++] = i;}cout << s << endl;}return 0; }總結(jié)
- 上一篇: I - 交叉排序(冒泡实现)
- 下一篇: git_12.13