이 자습서에서는 마지막 작업을 추가 및 삭제하여 한 문자열을 다른 문자열로 변환하는 프로그램에 대해 설명합니다.
이를 위해 두 개의 문자열이 제공됩니다. 우리의 임무는 마지막 요소를 추가하고 삭제하는 k 연산을 수행하여 첫 번째 문자열을 두 번째 문자열로 변환할 수 있는지 계산하는 것입니다.
예시
#include <bits/stdc++.h>
using namespace std;
//checking if conversion between strings is possible
bool if_convert(string str1, string str2,
int k){
if ((str1.length() + str2.length()) < k)
return true;
//finding common length of both string
int commonLength = 0;
for (int i = 0; i < min(str1.length(),
str2.length()); i++) {
if (str1[i] == str2[i])
commonLength++;
else
break;
}
if ((k - str1.length() - str2.length() +
2 * commonLength) % 2 == 0)
return true;
return false;
}
int main(){
str1 = "tutorials", str2 = "point";
k = 5;
cout << endl;
if (if_convert(str1, str2, k))
cout << "Yes";
else
cout << "No";
return 0;
} 출력
No