构造下一个字典排列算法的正确方法是什么?

时间:2019-07-15 12:25:10

标签: java algorithm permutation alphabetical

我看到了this个简单的算法,可以为用户输入的字符串查找下一个词典编排。

这是我的代码,

import java.util.*;
public class Next_Permutation{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        sc.nextLine();
        for(int h=0;h<t;h++){
            String s=sc.nextLine();
            char ch[]=s.toCharArray();    
            int i=ch.length-1;//LastIndex
            while(i>0 && (int)ch[i-1]>=(int)ch[i])//Find the longest non-increasing suffix(end appendage)
                i--;// Now, i is the start of the suffix
            if(i<=0||s.length()==1)
                System.out.println("no answer");//The given permutation is the last permutaion  
            else {
                //now,the leftmost element to i is the pivot, i.e i-1
                //Now, we find the rightmost element that exceeds the pivot
                int j=ch.length-1;
                while((int)ch[j]<=(int)ch[i-1])//exceed the pivot
                    j--;
                //Now, j is the new pivot which needs to be swapped with the old pivot
                char temp=' ';
                temp=ch[i-1];
                ch[i-1]=ch[j];
                ch[j]=temp;
                //Now, reverse the new/updated suffix
                while(i<j){
                    temp=ch[i];
                    ch[i]=ch[j];
                    ch[j]=temp;
                    i++;
                    j--;}
                System.out.println(String.valueOf(ch));}}}}

这在某些情况下有效,但在某些情况下也失败,例如:

对于字符串dkhc,它给出的是hdkc而不是hcdk。

为什么会显示这种不一致?如何解决此问题?

1 个答案:

答案 0 :(得分:0)

在反转新的更新后缀之前,请使用j = ch.length-1;更新变量j;

import java.util.*;
public class Next_Permutation{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        sc.nextLine();
        for(int h=0;h<t;h++){
            String s=sc.nextLine();
            char ch[]=s.toCharArray();    
            int i=ch.length-1;//LastIndex
            while(i>0 && (int)ch[i-1]>=(int)ch[i])//Find the longest non-increasing suffix(end appendage)
                i--;// Now, i is the start of the suffix
            if(i<=0||s.length()==1)
                System.out.println("no answer");//The given permutation is the last permutaion  
            else {
                //now,the leftmost element to i is the pivot, i.e i-1
                //Now, we find the rightmost element that exceeds the pivot
                int j=ch.length-1;
                while((int)ch[j]<=(int)ch[i-1])//exceed the pivot
                    j--;
                //Now, j is the new pivot which needs to be swapped with the old pivot
                char temp=' ';
                temp=ch[i-1];
                ch[i-1]=ch[j];
                ch[j]=temp;

                //Now, reverse the new/updated suffix
                j=ch.length-1;
                while(i<j){
                    temp=ch[i];
                    ch[i]=ch[j];
                    ch[j]=temp;
                    i++;
                    j--;
                }
                System.out.println(String.valueOf(ch));
          }
       }
    }
}