สมมติว่าเรามีสตริง S ที่มีอักขระ n ในโปรแกรมแก้ไขข้อความ มีกฎแปลก ๆ ตัวแก้ไขคำของโปรแกรมแก้ไขข้อความนี้ทำงานในลักษณะที่ตราบใดที่มีสระสองตัวติดต่อกันในคำ มันจะลบสระแรกในคำ หากไม่มีสระติดกัน 2 ตัวในคำ ให้ถือว่าถูก เราต้องหาคำที่แก้ไขจาก S. สระคือ 'a', 'e', 'i' 'o', 'u' และ 'y'
ดังนั้น ถ้าอินพุตเหมือน S ="แย่" ผลลัพธ์จะเป็น "พอ"
ขั้นตอน
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนเหล่านี้ -
n := size of S t := "aeiouy" for initialize i := 1, when i < n, update (increase i by 1), do: if S[i] is in t and S[i - 1] is in t, then: delete ith character from S (decrease i by 1) return S
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <bits/stdc++.h>
using namespace std;
string solve(string S){
int n = S.size();
string t = "aeiouy";
for (int i = 1; i < n; i++){
if (t.find(S[i]) != -1 && t.find(S[i - 1]) != -1){
S.erase(i, 1);
i--;
}
}
return S;
}
int main(){
string S = "poor";
cout << solve(S) << endl;
} อินพุต
"poor"
ผลลัพธ์
por