//通过一些测试用例,但没有确定完全正确,望发现者能够给予指正,感谢!
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
//计算 26^start + 26^(start+1) + ... + 26^endm
int compute26(int start,int endm){
int sum = 0,i;
//为满足此题需要这里返回1
if(start > endm){
return 1;
}
for (i = start;i <= endm;i ++)
{
//pow(x,y)求x的y次方
sum += pow(26,i);
}
return sum;
}
//输入两个字符串和长度,计算并返回结果
int ins1s2(){
int sum = 0,len = 0,i = 0,j;
string s1,s2;
cin>>s1>>s2>>len;
//字符串相等、字符串1大于字符串2、长度为0:返回0
if(s1 >= s2 || len == 0){
cout<<"相等"<<endl;
return sum;
}
//找到第一个不一样的字母
while(s1[i] == s2[i]){
i ++;
len --;
}
//长度为0:返回0
if (len == 0)
{
cout<<"长度为0"<<endl;
return sum;
}
//将从第一个相同位置不同字符到最后的字符串提取出来
if (i != 0){
j = s1.length()-i;
if (j == 0)
{
s1 = "";
}else{
s1 = s1.substr(i,s1.length()-i+1);
}
//满足s1<s2时,s2不会为空
s2 = s2.substr(i,s2.length()-i+1);
cout<<"S1:"<<s1<<endl;
cout<<"S2:"<<s2<<endl;
}
//从第二个字符开始比较,计算以s1[0]开始的符合条件的第1部分
if (s1.length() == 1)//当s1的长度==1
{
sum += compute26(1,len-1);
cout<<"走完S1:"<<sum<<endl;
}else//当s1的长度>=2时
{
for (i = 1;i < s1.length();i ++)
{
if (i == s1.length()-1)
{
sum += ('z'-s1[i]+1)*compute26(0,len-i-1);
//减去s1
sum --;
cout<<"走完S1:"<<sum<<endl;
}else{
sum += ('z'-s1[i])*compute26(0,len-i-1);
cout<<"-S1:"<<i<<" 此时:"<<sum<<endl;
}
}
}
//计算打头字母为s1[0]-s2[0]之间的第2部分
if (s1.length() == 0 && s2[0] != 'a')
{
sum += (s2[0]-'a')*compute26(0,len-1);
}else if ((s2[0]-s1[0]) != 1){
sum += (s2[0]-s1[0]-1)*compute26(0,len-1);
}
cout<<"-----中间:"<<sum<<endl;
//从第二个字符开始比较,计算以s2[0]开始符合条件的第3部分,当s2长度等于1不计算
for (i = 1;i < s2.length();i ++)
{
if (i == s2.length()-1)
{
sum += s2[i] - 'a';
//加上s2[0]
sum ++;
cout<<"走完S2:"<<sum<<endl;
}else{
sum += (s2[i]-'a')*compute26(0,len-i-1);
cout<<"-S2:"<<i<<" 此时:"<<sum<<endl;
}
}
return sum;
}
int main(){
//输出结果
cout<<ins1s2()<<endl;
return 0;
}
求满足 Str1<Str<Str2 且 0<Str.length <=len 的字符串个数
最新推荐文章于 2024-07-25 14:57:29 发布