Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不用STL,不求全排列,计数排序,O(N)时间复杂度,0MS AC#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator