#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define db(a) \
cout << #a << " = " << a << endl
#define db2(a, b) \
cout << #a << " = " << a << " " << #b << " = " << b << endl
#define inf (1<<30)
#define foreach(m, it) \
for (typeof(m.begin()) it = m.begin(); it != m.end(); it++)
using namespace std;
struct word {
string str; char trait[26];
bool operator >= (const word& other) {
int i = 0;
for (; i < 26 && trait[i] >= other.trait[i]; i++);
return i == 26;
}
word& operator -= (const word& other) {
for (int i = -1; ++i < 26; trait[i] -= other.trait[i]);
return *this;
}
word& operator += (const word& other) {
for (int i = -1; ++i < 26; trait[i] += other.trait[i]);
return *this;
}
};
void genTrait(const char *str, char * trait) {
for (fill(&trait[0], &trait[26], 0); *str != 0; ++trait[*str++ - 'A']);
}
typedef vector<word>::iterator iterpal;
void searchAnagram(iterpal ini, iterpal fin, word& frase) {
int i = 0;
string &str = frase.str;
for (; i < 26 && frase.trait[i] == 0; i++);
if (i == 26) {
istringstream ss(str);
vector<string> original, generado;
for (string word; ss >> word && word != "="; original.push_back(word));
for (string word; ss >> word ; generado.push_back(word));
sort(original.begin(), original.end());
if (original != generado) {
printf("%s\n", str.c_str());
}
return;
}
for (iterpal it = ini; it != fin; it++) {
if (frase >= *it) {
frase -= *it;
str.push_back(' ');
str.append(it->str);
searchAnagram(it + 1, fin, frase);
str.erase(str.length() - it->str.length() - 1);
frase += *it;
}
}
}
int main() {
#ifdef dennisbot
freopen("in.in", "r", stdin);
freopen("ou.out", "w", stdout);
#endif
vector<word> dict, subset;
char line[30];
for (word palabra; gets(line) != NULL && line[0] != '#'; ) {
palabra.str = line;
genTrait(line, palabra.trait);
dict.push_back(palabra);
}
for (string linea; getline(cin, linea) && linea != "#"; subset.clear()) {
word frase = {linea};
linea.erase(remove(linea.begin(), linea.end(), ' '), linea.end());
if (linea.empty()) continue;
genTrait(linea.c_str(), frase.trait);
for (iterpal it = dict.begin(); it != dict.end(); it++) {
if (frase >= *it) {
subset.push_back(*it);
}
}
frase.str.append(" =");
searchAnagram(subset.begin(), subset.end(), frase);
}
return 0;
}
martes, 17 de julio de 2012
148 - Anagram checker, uva
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario