ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อแปลงสตริงหนึ่งเป็นสตริงอื่นโดยใช้การผนวกและลบการดำเนินการล่าสุด
สำหรับสิ่งนี้เราจะมีให้สองสตริง งานของเราคือการคำนวณว่าสตริงแรกสามารถแปลงเป็นสตริงที่สองได้หรือไม่โดยดำเนินการ 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