Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

不用STL,不求全排列,计数排序,O(N)时间复杂度,0MS AC

Posted by ACKingSEU at 2013-05-22 10:34:30 on Problem 1146 and last updated at 2013-05-22 10:37:21
#include<stdio.h>
#include<string.h>

char buf[64];
char code[64];
int cnt[26];

int main()
{
    int i, k, len, pos;
    char* s;
    char min;

    while(gets(buf) != NULL && buf[0] != '#'){
        len = strlen(buf);
        for(i = len; i > 0; --i){
            if(buf[i-1] < buf[i]){
                break;
            }
        }
        if(i == 0){
            printf("No Successor\n");
            continue;
        }

        min = 'z';
        k = i;
        while(k <= len){
            if(buf[k] > buf[i-1] && buf[k] < min){
                min = buf[k];
                pos = k;
            }
            ++k;
        }
        buf[pos] = buf[i-1];
        buf[i-1] = min;
        strcpy(code, buf + i);
        buf[i] = '\0';
        memset(cnt, 0, sizeof(cnt));
        s = code;
        while(*s){
            ++cnt[*s - 'a'];
            ++s;
        }
        k = 0;
        for(i = 0; i < 26; ++i){
            while(cnt[i] > 0){
                code[k++] = i + 'a';
                --cnt[i];
            }
        }
        code[k] = '\0';
        printf("%s%s\n", buf, code);
    }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator