Xiaoxue had read a study before, which stated: "The arrangement of English letters does not affect the reading order." So, Xiaoxue decided to verify the truth of this study. She would provide a string before flipping and the axis of flipping. The string before flipping would be flipped left and right according to the axis and the order would be reversed.
For example, if Xiaoxue provides the string "foreign" and uses "re" as the axis of flipping, then "re" stays in place, and the "fo" on its left and "ign" on its right swap positions, resulting in "ignrefo". Then, "fo" and "ign" are reversed individually, resulting in the flipped string "ngireof".
Please write a program to help Xiaoxue find the string after flipping, given the original string and the flip axis.
Sample Input(s) | Sample Output(s) |
---|---|
The first line will input a string, representing the string S before flipping, and this string has a maximum length of 50 characters. The second line will input a string, representing the flip axis string T. The S string must contain the T string. If the S string appears more than once in the T string, the leftmost occurrence will be the flip axis (see Example 2). Note: All inputs are lowercase English letters. | According to the flipping rule, output the flipped string in one line. |
apple ppl | eppla |
banana na | annaab |
split t | tilps |
what wh | tawh |
complex ex | exlpmoc |
overcome overcome | overcome |
Thought Process
First, locate the position of the flipping axis. Then, divide the string into three equal parts: the part before the flipping axis, the flipping axis itself, and the part after the flipping axis. Reverse the strings before and after the flipping axis and swap their positions to get "part after flipping axis -> flipping axis -> part before flipping axis".
Sample Code-ZeroJudge F341: Reading
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string str, mid;
cin >> str >> mid;
for (int i = 0; i<str.length(); i++) {
if (str[i] == mid[0]) {
string tmp = "";
for (int j = i; j<i+mid.length(); j++) tmp += str[j];
if (tmp == mid) {
string a = "", b = "";
for (int j = 0; j<i; j++) a += str[j];
for (int j = i+mid.length(); j<str.length(); j++) b += str[j];
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
cout << b << mid << a << "\n";
break;
}
}
}
}
//ZeroJudge F341
//Dr. SeanXD