Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 构建下一个字典排列算法的正确方法是什么?_Java_Algorithm_Permutation_Alphabetical - Fatal编程技术网

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

Java 构建下一个字典排列算法的正确方法是什么?,java,algorithm,permutation,alphabetical,Java,Algorithm,Permutation,Alphabetical,我看到了为用户输入的字符串查找下一个字典排列的简单算法 这是我的密码 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

我看到了为用户输入的字符串查找下一个字典排列的简单算法

这是我的密码

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));}}}}
import java.util.*;
公共类下一个置换{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
sc.nextLine();
对于(int h=0;h0&&(int)ch[i-1]>=(int)ch[i])//查找最长的非递增后缀(结束附录)
i--;//现在,i是后缀的开头

如果(i在反转新更新的后缀之前,用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));
          }
       }
    }
}
import java.util.*;
公共类下一个置换{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
sc.nextLine();
对于(int h=0;h0&&(int)ch[i-1]>=(int)ch[i])//查找最长的非递增后缀(结束附录)
i--;//现在,i是后缀的开头

如果(i在反转新更新的后缀之前,用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));
          }
       }
    }
}
import java.util.*;
公共类下一个置换{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
sc.nextLine();
对于(int h=0;h0&&(int)ch[i-1]>=(int)ch[i])//查找最长的非递增后缀(结束附录)
i--;//现在,i是后缀的开头

if(i)最外面的循环
的目的是什么(int h=0;h@m.raynal它只在一个程序中运行t个测试用例最外面的循环
的目的是什么(inth=0;h@m.raynal它只是在一个程序中运行t个测试用例