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

不用next_permutation的做法

Posted by Liuzhaoliang at 2014-10-05 14:00:33 on Problem 3785
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int P,k;
    scanf("%d",&P);
    char s[100];
    while(P--){
        scanf("%d %s",&k,s);
        int n = strlen(s);
        int ii = -1;
        for(int i=n-1;i>0;i--)
            if(s[i]>s[i-1]){
                ii = i;
                break;
            }
        printf("%d ",k);
        if(ii==-1)printf("BIGGEST\n");
        else{
            int best = '9';
            int x;
            for(int i=ii;i<n;i++)
                if(s[i]>s[ii-1] && best>=s[i]) best = s[i],x=i;
            swap(s[ii-1],s[x]);
            sort(s+ii,s+n);
            printf("%s\n",s);
        }
    }
    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