-
1120 문자열Algorithm/baekjoon 2022. 2. 11. 23:31
1120번: 문자열
길이가 N으로 같은 문자열 X와 Y가 있을 때, 두 문자열 X와 Y의 차이는 X[i] ≠ Y[i]인 i의 개수이다. 예를 들어, X=”jimin”, Y=”minji”이면, 둘의 차이는 4이다. 두 문자열 A와 B가 주어진다. 이때, A의
www.acmicpc.net
실버 4인 문제....!
여러번에 걸쳐서 풀었다.
솔루션을 안보고 온전히 내 힘으로 풀었다는 뿌듯함이 남은 문제이다ㅎㅎ
컴파일에러가 떴었는데,
string을 char형 배열로 바꿔주는 strcpy()를 사용할 때는 #include<cstring>을 해줘야 하더라.
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<cstring> using namespace std; int main() { cin.tie(NULL); cin.sync_with_stdio(false); string str1; string str2; cin >> str1 >> str2; char arr1[50]; strcpy(arr1, str1.c_str()); //for (int i = 0; i < 10; i++) // cout<<arr1[i]; char arr2[50]; strcpy(arr2, str2.c_str()); //for (int i = 0; i < 10; i++) // cout << arr2[i]; int len1 = 0; int len2 = 0; while (true) { len1++; if (arr1[len1] == '\0') break; } //cout << "len1 " << len1 << "\n"; while (true) { len2++; if (arr2[len2] == '\0') break; } //cout << "len2 " << len2 << "\n"; int index = 0; int cnt = 0; int min = 100; for (int j = 0; j < len2 - len1 + 1; j++) { cnt += j; //cout << "앞부분 차이만큼 j더하기 j는" << j << "\n"; index = 0; cnt = 0; while (true) { if (arr2[index] == '\0') break; if (arr1[index] == '\0') { //cnt += len2 - len1 - j; break; } if (arr1[index] == arr2[index+j]) { //cout <<j << "일때 같은문자발생" << "\n"; } else{ cnt++; } index++; } //if (cnt < min) // min = cnt; //cout << min<<"\n"; //cout << cnt << "\n"; if (cnt < min) min = cnt; } cout << min; }
'Algorithm > baekjoon' 카테고리의 다른 글
1924 2007년 (0) 2022.02.12 2028 자기복제수 (0) 2022.02.12 05 스택 (0) 2022.01.26 04 연결리스트- 백준 5397 (0) 2022.01.17 03 배열 - 백준 2577/백준 1475/백준 3273 (0) 2022.01.08