题目描述
求字典序在s1和s2之间的,长度在len1到len2的字符串的个数,结果mod 1000007。
输入描述:
每组数据包涵s1(长度小于100),s2(长度小于100),len1(小于100000),len2(大于len1,小于100000)
输出描述:
输出答案。
输入例子:
ab ce 1 2
输出例子:
56
这个题目我做了半天。后来发现是很简单的。其实可以想象成26进制问题,然后看看两个数之间差多少位。不过要注意的是,每个长度又应该算一次。而不是整体算一次。举个例子
ab ce
结果中包含b c。。但是b c的序小于ab。所以我们要分两次计算。一次
a c之间序差多少,这里包括c。
ab ce之间的序差多少。。
而最后一次计算ce的时候计算方式是包括最后一个数的,所以我们要在最后结果减去1.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String s = scan.nextLine();
String[] array = s.split(" ");
int min = Integer.parseInt(array[2]);
int max = Integer.parseInt(array[3]);
System.out.println(getStrCount(array[0],array[1],min,max));
}
}
public static long getStrCount(String str1,String str2,int len1,int len2){
Long result=0L;
char a[] = str1.toCharArray();
char b[] = str2.toCharArray();
int i = len1;
for(i = len1; i <= len2; i++){//长度从len1 到len2,共有len2-len1种情况
Long indexA=0L;
int min=Math.min(i, a.length);
for(int j=0;j<min;j++)
{
indexA=indexA*26+a[j]-'a'+1;
}
if(i>a.length)
{
int temp=i-a.length;
while(temp-->0)
{
indexA*=26;
}
}
Long indexB=0L;
for(int j=0;j<i;j++)
{
indexB=indexB*26+b[j]-'a'+1;
}
result+=indexB-indexA;
}
return (result-1)% 1000007;
}
}