题目相关
题目链接
AtCoder Beginner Contest 177 B 题,https://atcoder.jp/contests/abc177/tasks/abc177_b。
Problem Statement
Given are two strings S and T.
Let us change some of the characters in S so that T will be a substring of S.
At least how many characters do we need to change?
Here, a substring is a consecutive subsequence. For example, xxx
is a substring of yxxxy
, but not a substring of xxyxx
.
Input
Input is given from Standard Input in the following format:
S
T
Output
Print the minimum number of characters in S that need to be changed.
Samples1
Sample Input 1
cabacc
abc
Sample Output 1
1
Explaination
For example, changing the fourth character a
in S to c
will match the second through fourth characters in S to T.
Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.
Samples2
Sample Input 2
codeforces
atcoder
Sample Output 2
6
Constraints
- The lengths of S and T are each at least 1 and at most 1000.
- The length of T is at most that of S.
- S and T consist of lowercase English letters.
题解报告
题目翻译
给定两个字符串 S 和 T。我们改变字符串 S 中的某些字符,这样字符串 T 就变成 S 的子串。
问最少改变字符可以达到这个要求。
题目分析
首先我们要明白,每个题解只是针对特定的题目,所以我们先关心数据范围。从题目的描述可以看到,字符串的最小长度为 1,最大长度为 1000,因此暴力枚举可以完成任务,不需要使用更高级的字符串匹配算法。
所谓的暴力枚举,就是从字符串 S 从 0 开始,逐一枚举字符串 T 的长度。将所有的修改可能计算出,取其中最小的。
样例数据分析
样例数据 1 分析
根据样例数据,我们知道 S=cabacc,T=abc,这样 lenS 为 6,lenT 为 3。初始化 ans(最终解)为 1e3(参考数据范围)。
1、第一次枚举。
从字符串 S 的第 0 个位置开始,向后枚举 3 个位置。设置变量 change(表示从这个位置开始更换,最少要变几个字母) 值为 0。
S[0]=‘c',T[0]='a',两个字符不相等,change 变为 1,表示需要更换一个字母。
S[1]=‘a',T[1]='b',两个字符不相等,change 变为 2,表示需要更换两个字母。
S[2]=‘b',T[2]='c',两个字符不相等,change 变为 3,表示需要更换一个字母。
意味着,如果我们从字符串 S 的第 0 个位置开始更换字母,我们需要变换 3 个字母才能得到子串 T。这样我们设置 ans 为 ans(值为 1e3) 和 change(值为 3) 的最小值,也就是 ans=3。
2、第二次枚举
从字符串 S 的第 1 个位置开始,向后枚举 3 个位置。设置变量 change(表示从这个位置开始更换,最少要变几个字母) 值为 0。
S[1]=‘a',T[0]='a',两个字符相等,change 数据不变。
S[1]=‘b',T[1]='b',两个字符相等,change 数据不变。
S[2]=‘a',T[2]='c',两个字符不相等,change 变为 1,表示需要更换一个字母。
意味着,如果我们从字符串 S 的第 2 个位置开始更换字母,我们需要变换 1 个字母才能得到子串 T。这样我们设置 ans 为 ans(值为 3) 和 change(值为 1) 的最小值,也就是 ans=1。
3、第三次枚举
从字符串 S 的第 3 个位置开始,向后枚举 3 个位置。设置变量 change(表示从这个位置开始更换,最少要变几个字母) 值为 0。
S[2]=‘b',T[0]='a',两个字符不相等,change 变为 1,表示需要更换一个字母。
S[3]=‘a',T[1]='b',两个字符不相等,change 变为 2,表示需要更换一个字母。
S[4]=‘c',T[2]='c',两个字符相等,change 数据不变。
意味着,如果我们从字符串 S 的第 3 个位置开始更换字母,我们需要变换 2 个字母才能得到子串 T。这样我们设置 ans 为 ans(值为 1) 和 change(值为 2) 的最小值,也就是 ans=1。
4、第四次枚举
从字符串 S 的第 4 个位置开始,向后枚举 3 个位置。设置变量 change(表示从这个位置开始更换,最少要变几个字母) 值为 0。
S[3]=‘a',T[0]='a',两个字符相等,change 数据不变。
S[4]=‘c',T[1]='b',两个字符不相等,change 变为 1,表示需要更换一个字母。
S[5]=‘c',T[2]='c',两个字符相等,change 数据不变。
意味着,如果我们从字符串 S 的第 4 个位置开始更换字母,我们需要变换 1 个字母才能得到子串 T。这样我们设置 ans 为 ans(值为 1) 和 change(值为 1) 的最小值,也就是 ans=1。
5、第五次枚举
从字符串 S 的第 5 个位置开始,向后枚举 3 个位置。由于字符串 S 的长度不够向后枚举 3 个字符,因此不需要进行枚举。
6、第六次枚举
从字符串 S 的第 6 个位置开始,向后枚举 3 个位置。由于字符串 S 的长度不够向后枚举 3 个字符,因此不需要进行枚举。
这样,我们得到最终的答案为 1。
样例数据 2 分析
类似样例数据 1 分析,我就偷懒不写了。
时间复杂度分析
根据样例数据分析,我们可以知道,这样的暴力枚举算法时间复杂度为 O(N^2)。
根据题意,The lengths of S and T are each at least 1 and at most 1000,我们知道采用暴力枚举的方法,但是对于本题而言,完全可以满足需求。
如果题目将数据范围进一步扩大,暴力枚举算法肯定就是 TLE,需要采用高级的字符串算法,如 KMP,这里我们不做分析。
AC 参考代码
//https://atcoder.jp/contests/abc177/tasks/abc177_b
#include <iostream>
#include <string>
using namespace std;
int main() {
string s, t;
cin>>s>>t;
int lenS=s.length();
int lenT=t.length();
int ans=1e4;
for (int i=0; i+lenT<=lenS; i++) {
int change=0;
for (int j=0; j<lenT; j++) {
if (s[i+j]!=t[j]) {
change++;
}
}
ans = min(ans, change);
}
cout<<ans<<"\n";
return 0;
}