字符串比较
题⽬描述:
给定字符串 A , B 和正整数 V , A 的⻓长度与 B 的⻓长度相等,请计算 A 满⾜如下条件的最⼤ 连续⼦串的⻓长度: 1、该连续⼦串在 A 和 B 中的位置和⻓长度均相同。 2、该连续⼦串 |A[i]-B[i]| 之和⼩于等于 V ,其中 |A[i]-B[i]| 表示两个字⺟ ASCII 码之差的绝对值。 输⼊描述: 输⼊为三⾏: 第⼀⾏为字符串 A ,仅包含⼩写字符, 。 第⼀⾏为字符串 B ,仅包含⼩写字符, 。 第⼀⾏为正整数 V , 。
输出描述:
字符串最⼤连续⼦串的⻓长度,要求该⼦串 |A[i]-B[i]| 之和⼩于等于 V 。
输⼊:
xxcdefg
cdefghi
5
输出:
2
code
- (未经验证
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string A,B;
int V;
cin >>A>>B>>V;
int len=A.size();
int res=0;
for(int i=0; i<len; ++i){
for(int j=1; i+j<len; ++j){
int sum=0;
//计算差值
for(int k=i;k<i+j;++k){
sum += fabs(A[k]-B[k]);
}
if(sum<=V){
res = max(res, j);
}
}
}
printf("%d\n",res);
return 0;
}