Each set of input consists of two lines of strings without spaces and with characters ranging from ASCII code 33 to 126. Please tell me, after appropriate reordering, will the two strings become the same?
For example, "e83k" can be rearranged as "38ek" or "e3k8," among 15 other permutations.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF input, with each line containing a string. Terminate when "STOP!!" is read. The maximum length of the string is 1,000,000 characters. | If the two strings can become the same, output "yes"; otherwise, output "no." |
e83k 38ek asdfghjkl;’ ‘;lkjhgfdsa 1234 4521 SToP!! stop!! STOP!! | yes yes no no |
Thought Process
For this problem, it's recommended to use cin optimization or scanf to avoid the time limit being exceeded (TLE). After receiving two sets of strings, it's efficient to first check if their lengths are the same. If not, output "no" directly to save time. Then, you can use a loop to process each character in the strings. Utilize a map to store the frequency of each character in both strings. Let's name the two strings A and B. For string A, increment the count of each character in the map, and for string B, decrement the count of each character in the map. Afterward, iterate over the map using a for loop (for (auto it: Map)) to check if the count of any character is not zero. If so, output "no" and break out of the loop to save time. If all counts are zero, output "yes."
Sample Code-ZeroJudge A275: Modifying Strings
#include <iostream>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string a;
while (cin >> a && a != "STOP!!")
{
string b;
cin >> b;
if (a.length() != b.length()) cout << "no\n";
else
{
map<char, int>MAP;
for (int i = 0; i<a.length(); i++)
{
MAP[a[i]]++;
MAP[b[i]]--;
}
bool yes = true;
for (auto it:MAP)
{
if (it.second != 0)
{
cout << "no\n";
yes = false;
break;
}
}
if (yes) cout << "yes\n";
}
}
}
//ZeroJudge A275
//Dr. SeanXD