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
先找到翻轉軸的位置,然後將字串切成三等份,分別為翻轉軸前、翻轉軸、和翻轉軸後。將前後字串進行翻轉並將順序對調,會變成「翻轉軸後 –> 翻轉軸 –> 翻轉軸前」。
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