Created
November 12, 2019 15:44
-
-
Save WindAzure/9b01234993f071255623d5ae4b2abb82 to your computer and use it in GitHub Desktop.
UVa 156
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<pair<string, string>> ananagramTable; | |
map<string, bool> relativeAnanagram; | |
string compressWordToAppearanceCode(string word) | |
{ | |
auto appearanceCode = vector<int>(26, 0); | |
for (auto character: word) | |
{ | |
appearanceCode[tolower(character) - 'a']++; | |
} | |
auto appearanceCodeString = string {}; | |
transform(appearanceCode.begin(), appearanceCode.end(), back_inserter(appearanceCodeString), | |
[](int characterAppearance) { return characterAppearance + '0'; }); | |
return appearanceCodeString; | |
} | |
void addRecord(string word) | |
{ | |
auto appearanceCode = compressWordToAppearanceCode(word); | |
if (relativeAnanagram.count(appearanceCode) == 0) | |
{ | |
relativeAnanagram[appearanceCode] = true; | |
ananagramTable.emplace_back(appearanceCode, word); | |
} | |
else | |
{ | |
relativeAnanagram[appearanceCode] = false; | |
} | |
} | |
void printResult() | |
{ | |
sort(ananagramTable.begin(), ananagramTable.end(), | |
[](const pair<string, string> &ananagramElement1, const pair<string, string> &ananagramElement2) { | |
return ananagramElement1.second < ananagramElement2.second; | |
}); | |
for (auto ananagram: ananagramTable) | |
{ | |
if (relativeAnanagram[ananagram.first]) | |
{ | |
cout << ananagram.second << endl; | |
} | |
} | |
} | |
int main() | |
{ | |
auto word = string {}; | |
while (cin >> word) | |
{ | |
if (word == "#") | |
{ | |
break; | |
} | |
addRecord(word); | |
} | |
printResult(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment