字符串计数

题目描述
求字典序在s1和s2之间的,长度在len1到len2的字符串的个数,结果mod 1000007。
输入描述:
每组数据包涵s1(长度小于100),s2(长度小于100),len1(小于100000),len2(大于len1,小于100000)
输出描述:
输出答案。

示例1
输入
ab ce 1 2

输出
56
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main(){
    string str1,str2;
    int len1,len2;
    while( cin >> str1 >> str2 >> len1 >> len2 ){
        int i,j,l1,l2,num = 0;
        char bb = 96;
        if(str1.length() < len2 )
            str1.append(len2 - str1.size(),bb);
        if(str2.length() < len2 )
            str2.append(len2 - str2.size(),bb);
        for(i=len1;i<=len2;i++)
            for(j=0;j<i;j++)
                num = num + (str2[j] - str1[j])*pow(26,i-j-1);
        cout << num-1 <<endl;
    }
}

这个也可以通过


#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main(){
    string str1,str2;
    int len1,len2;
    while( cin >> str1 >> str2 >> len1 >> len2 ){
        int i,j,l1,l2,num = 0;
       /* char bb = 96;
        if(str1.length() < len2 )
            str1.append(len2 - str1.size(),bb);
        if(str2.length() < len2 )
            str2.append(len2 - str2.size(),bb);*/
        for(i=len1;i<=len2;i++)
            for(j=0;j<i;j++)
                num = num + (str2[j] - str1[j])*pow(26,i-j-1);
        cout << num-1 <<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值