Bài toán. Cho hai xâu ký tự s1, s2. Hãy đếm số lần xuất hiện của xâu s1 trong xâu s2 đồng thời xóa các xâu s1 có trong s2. Ví dụ:
DEMKT.INP |
DEMKT.OUT |
ab abxytabuvrbabxy |
3 xytuvrbxy |
Code tham khảo:
#include <iostream>
#include <cstring>
using namespace std;
int main() {
freopen("DEMKT.INP","r",stdin);
freopen("DEMKT.OUT","w",stdout);
string s1, s2;
getline(cin, s1);
getline(cin, s2);
int dem = 0;
while(s1.find(s2) >= 0)
{
int p = s2.find(s1);
if(p == -1) break;
dem++;
s2.erase(p, s1.length());
}
cout << dem << endl << s2;
return 0;
}