알고리즘
[C++] 백준 1120번 문자열
J3SUNG
2019. 9. 26. 02:07
728x90
s1과 s2를 비교해서 s1에 앞과 뒤에 문자를 추가해서 차이가 가장 적게 만드는 문제입니다.
s1과 s2가 가장 겹치는 곳을 찾아서 카운트 해줍니다.
그리고 카운트에 둘간의 길이 차이를 더해줍니다. (s1의 앞과 뒤에 문자를 추가해주는 경우)
s2의 길이에서 카운트를 뺀 만큼이 차이값이므로 출력해줍니다.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int sub;
int count;
int result = 0;
string s1;
string s2;
cin >> s1 >> s2;
sub = s2.length() - s1.length();
for (int i = 0; i <= sub; ++i) {
count = 0;
for (int j = 0; j < s1.length(); ++j) {
if (s1[j] == s2[j + i]) {
++count;
}
}
result = max(result, count);
}
result += sub;
result = s2.length() - result;
cout << result << endl;
return 0;
}