牛牛手里有一个字符串A,羊羊的手里有一个字符串B,B的长度大于等于A,所以牛牛想把A串变得和B串一样长,这样羊羊就愿意和牛牛一起玩了。
而且A的长度增加到和B串一样长的时候,对应的每一位相等的越多,羊羊就越喜欢。比如"abc"和"abd"对应相等的位数为2,为前两位。
牛牛可以在A的开头或者结尾添加任意字符,使得长度和B一样。现在问牛牛对A串添加完字符之后,不相等的位数最少有多少位?
第一行为字符串A,第二行为字符串B,A的长度小于等于B的长度,B的长度小于等于50.字符均为小写字母。
输出一个整数表示A串添加完字符之后,不相等的位数最少有多少位?
abe cabc
1
#include<iostream>
using namespace std;
int longestfix(string a, string b)
{
int m=0;
for(int j=0; j<=b.size()-a.size(); j++)
{
int num=0;
for(int i=0; i<a.size(); i++)
if(a[i]==b[i+j])
num++;
m=max(num,m);
}
return a.si***t main()
{
string a,b;
cin>>a>>b;
cout<<longestfix(a,b)<<endl;
return 0;
} #include <bits/stdc++.h>
using namespace std;
int main() {
string A, B;
cin >> A >> B;
int n = A.size(), m = B.size();
int ans = n;
for(int left = 0; left <= m - n; left++) {
int right = left + n - 1;
int cnt = 0;
for(int i = left; i <= right; i++) {
cnt += A[i - left] != B[i];
}
ans = min(ans, cnt);
}
cout << ans << endl;
return 0;
}
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 处理输入
String str1 = in.next(), str2 = in.next();
// tmp来枚举出str1在str2中不同位置相同字符的最大数量
int tmp = str2.length() - str1.length(), max = -1;
while (tmp != -1) {
// 每次循环需要清空变量,所以要放在while()里面
int j = tmp, count = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == str2.charAt(j)) count++;
j++;
}
// max存储相同字符的最大值
max = Math.max(max, count + (str2.length() - str1.length()));
tmp--;
}
// 处理输出
System.out.println(str2.length() - max);
}
} #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int a=0,b=0;
int maxlen=0,maxlen1=0;
char *sa=(char*)malloc(sizeof(char)*100);
char *sb=(char*)malloc(sizeof(char)*100);
scanf("%s",sa);
scanf("%s",sb);
a=strlen(sa);
b=strlen(sb);
for(int i=0;i<b-a+1;i++)
{
maxlen1=0;
for(int j=0;j<a;j++)
{
if(*(sa+j)==*(sb+i+j))
maxlen1++;
}
maxlen=maxlen<maxlen1?maxlen1:maxlen;
}
printf("%d",a-maxlen);
return 0;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String a = in.nextLine();
String b = in.nextLine();
int longs = longs(a, b);
System.out.println(longs);
}
public static int longs(String a,String b){
//转换为char类型数组
char[] arrayA = a.toCharArray();
char[] arrayB = b.toCharArray();
int result=0;
//字符a,b之间的差值
for (int i = 0; i <= arrayB.length-arrayA.length; i++) {
//对应位相等的个数
int max=0;
for (int j = 0; j < arrayA.length; j++) {
//如果对应位相等
if (arrayA[j]==arrayB[j+i]){
max++;
}
}
result=Math.max(result,max);
}
return arrayA.length-result;
}
}