hihoCoder太阁最新面经算法竞赛5 register

Ended

Participants:118

Verdict:Accepted
Score:100 / 100
Submitted:2016-06-20 14:57:26

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <algorithm>
using namespace std;
char str[100010];
char ans[100010];
int cnt[128];
int cnt2[128];
int main()
{
    scanf("%s", str);
    char *p = str;
    while (*p)
        cnt[*p]++, cnt2[*p++]++;
    int res = 0;
    for (int i = 'a'; i <= 'z'; i++)
        if (cnt[i])
            ++res;
    int cur = 0;
    int last = -1;
    int len = 0;
    while (res > 1)
    {
        while (cnt[cur + 'a'] == 0 || cur == last)
            cur = (cur + 1) % 26;
        cnt[cur + 'a']--;
        if (cnt[cur + 'a'] == 0)
            res--;
        ans[len++] = cur + 'a';
        last = cur;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX