This problem is very straightforward to solve, just pay attention to implementation details.
|
This problem is very straightforward to solve, just pay attention to implementation details.
|
This is a search problem and the key is to figure out the search space. Given the input number, we can generate all possible combinations of the valid words and use these words as search space. The other approach is to generate all possible numbers from the input dictionary and then use these numbers as search space. Obviously, given the input constraints (a list of fewer than 5,000 acceptable cattle names), the number search space is more practical.
using namespace std;
string name2number(string name){
char c = 0;
int value = 0;
ostringstream buf;
for(auto c : name){
if(c > 'Q')
value = (c - 1 - 'A') / 3 + 2;
else
value = (c - 'A') / 3 + 2;
buf << value;
}
return buf.str();
}
int main() {
ofstream fout("namenum.out");
ifstream fin("namenum.in");
ifstream dict_fin("dict.txt");
string value;
fin >> value;
string name;
bool not_found = true;
while(dict_fin >> name){
string number = name2number(name);
if(number != value)
continue;
fout << name << endl;
not_found = false;
}
if(not_found)
fout<<"NONE"<<endl;
dict_fin.close();
fin.close();
fout.close();
return 0;
}
Brutal force search… and a Hexo bug that prevent syntax being highlighted!
|