超排序

来源:互联网 发布:苹果手机4g网络用不了 编辑:程序博客网 时间:2024/04/29 02:18
          超排序

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

bLue 在跨年之际获得了一份小礼物,里面装了一串只包含大写字母和小写字母的字符串,如果你能帮 bLue 把这个字符串按照字典序排序(按 ASCII 码从小到大排序。大写字母的 ASCII 码小于小写字母的 ASCII 码),他会奖励你一个 Accepted。
Input

输入数据有多组(数据组数不超过 50),到 EOF 结束。

对于每组数据,输入一行只包含大写字母和小写字母的字符串,且长度不超过 1000000。
Output

对于每组数据,输出一行排序后的字符串。
Example Input

HappyNewYear
aaabAAbbBcdAB

Example Output

HNYaaeepprwy
AAABBaaabbbcd

Hint

由于数据量较大,不推荐直接使用 cin, cout 输入输出。

另外,请确保最终结果是直接输出整个字符串,而非使用 printf(“%c”) 或 putchar() 等函数一个一个地输出字符,否则可能导致超时。
Author
「SDUT Round #1 - Hello 2017 跨年大作战」bLue
分析:
看到这道题目,我的第一想法是快排,就直接用了才C++的快排函数,结果可想而知, TLE。
Time Limit Exceeded :

#include <bits/stdc++.h>using namespace std;int main(){    char m[1234561];    int n;    while(~scanf("%s", m))    {    n = strlen(m);    sort(m, m+n);    puts(m);    }    return 0;}/***************************************************User name: Result: Time Limit ExceededTake time: 1010msTake Memory: 0KBSubmit time: ****************************************************/

接着,我就想,快排都超时,还有什么方法吗?其实,我想到了桶排,不过,很快否定了,快排都超,更何况是桶排,又不能一个一个的输出……
后来,终于知道了啥是超排序……(桶排+快排)……
Accepted:

#include <bits/stdc++.h>using namespace std;char st1[1212121];int book[1211];int main(){    while(~scanf("%s", st1))    {       int x;       int len = strlen(st1);       for(int i=0;i<len;i++)       {         x = (int)st1[i];         book[x]++;       }       int k = 0;       for(int i='A';i<='Z';i++)       {          for(int j=0;j<book[i];j++)          {            st1[k++] = i;          }       }       for(int i='a';i<='z';i++)       {          for(int j=0;j<book[i];j++)          {            st1[k++] = i;          }       }       puts(st1);       memset(st1, ' ', sizeof(st1));       memset(book, 0, sizeof(book));    }    return 0;}/***************************************************User name:Result: AcceptedTake time: 260msTake Memory: 1428KBSubmit time: ****************************************************/
原创粉丝点击