小摩手里有一个字符串A,小拜的手里有一个字符串B,B的长度大于等于A,所以小摩想把A串变得和B串一样长,这样小拜就愿意和小摩一起玩了。
而且A的长度增加到和B串一样长的时候,对应的每一位相等的越多,小拜就越喜欢。比如"abc"和"abd"对应相等的位数为2,为前两位。
小摩可以在A的开头或者结尾添加任意字符,使得长度和B一样。现在问小摩对A串添加完字符之后,不相等的位数最少有多少位?
第一行 为字符串A,第二行 为字符串B, A的长度小于等于B的长度,B的长度小于等于100。 字符均为小写字母。
输出一行整数表示A串添加完字符之后,A B 不相等的位数最少有多少位?
abe cabc
1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author wylu
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] a = br.readLine().toCharArray();
char[] b = br.readLine().toCharArray();
int n = b.length - a.length;
int res = Integer.MAX_VALUE;
for (int i = 0; i <= n; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] != b[j + i]) count++;
}
res = Math.min(res, count);
}
System.out.println(res);
}
}
import java.util.*;
public class Main {
private static final int INT_MAX = 0X3f3f3f3f;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] a = sc.next().toCharArray(), b = sc.next().toCharArray();
int ans = INT_MAX;
for (int i=0; i<=b.length-a.length; i++) {
int diff = 0;
for (int j=0; j!=a.length; j++) {
if (a[j] != b[i+j]) { diff++; }
}
ans = Math.min(diff, ans);
}
System.out.println(ans);
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a,b;cin>>a>>b;
int len=a.length(),maxn=INT_MIN;
for(int i=0;i<=b.length()-len;i++)
{
string t=b.substr(i,len);
int sum=0;
for(int j=0;j<len;j++)
if(t[j]==a[j]) sum++;
maxn=max(maxn,sum);
}
cout<<len-maxn;
} A = input().strip()
B = input().strip()
dp = {}
def dfs(a, b):
if len(a) == len(b):
n = len(a)
st = 0
count = 0
while st<n:
if a[st] != b[st]:
count += 1
st += 1
return count
if dp.get(b):
return dp[b]
ans = float('inf')
ans = min(ans, dfs(a, b[1:]), dfs(a, b[:-1]))
dp[b] = ans
return ans
print(dfs(A, B)) import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
char[] A = input.nextLine().toCharArray();
char[] B = input.nextLine().toCharArray();
int len = B.length - A.length;
int res = Integer.MAX_VALUE;
for(int i = 0;i<=len;i++){
int count = 0;
for(int j = 0;j<A.length;j++){
if(A[j] !=B[j+i])
count++;
}
res = Math.min(res,count);
}
System.out.println(res);
}
}
答案都这么写吗。。好吧是我想复杂了。。我以为题目中abc和abd这个例子还必须要顺序相同。。有没有跟我这么想得。。😂