一个字符串的字典序列的排序号求解

     问题描述:加入给定一个字符串,如:“abcdefghijkl",其现在的排序为字典的正序排序,排序号为1,那么,字符串“abdcfelkjihg”的字典序号是多少呢?

    

     实例:

       输入:2

                   abcdefghijkl

                   lkjihgfedcba

     

       输出:

                  1

                  439084800



代码如下(未考虑溢出情况):


                                package dictionalSort;


import java.util.Scanner;


public class DictionSort {


public static void main(String[] args) {
String strModel="abcdefghijkl",temp;
temp=strModel;
int n=0,i=0;
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();
scanner.nextLine();
String[] strs=new String[n];
while(n>0){
strs[i++]=scanner.nextLine();
n--;
}//read data end

double sortNum=jieCheng(strModel.length());//母字符串的阶乘
int sum=0;
for(int j=0;j<strs.length;j++){   //n个输入串
        sum=0;
while(strs[j].length()>0){//每个输入串
sum+=(numIndex(strModel,strs[j].charAt(0))-1)*jieCheng(strModel.length()-1);

StringBuffer sb=new StringBuffer(strModel);
sb.deleteCharAt(numIndex(strModel,strs[j].charAt(0))-1);//删除strModel的检查字符
strModel=sb.toString();

StringBuffer sb1=new StringBuffer(strs[j]);
sb1.deleteCharAt(0);
strs[j]=sb1.toString();
}

System.out.println(++sum);

strModel=temp;

}//n个输入串 end




}
private static int numIndex(String model,char ch){
return model.indexOf(ch)+1;
}

//求阶乘的方法
private static double jieCheng(int i){
if(i==0) return 0;
if(i==1){
return i;
}else{
return i*jieCheng(--i);
}
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值