Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <iterator>
- using namespace std;
- class Node{
- public:
- char ch;
- vector<Node*> children;
- bool isWord;
- Node(char c) : ch(c), isWord(false), children(0) {}
- };
- class BOR{
- Node* root;
- public:
- BOR() :root(new Node('-')){}
- void add(string str)
- {
- Node* node = root;
- bool flag = false;
- for (int i = 0; i < str.length();)
- {
- char cur = str[i];
- for (int j = 0; j < node->children.size() && !flag; j++)
- {
- if (node->children[j]->ch == cur)
- {
- node = node->children[j];
- // a=(Node*)node->children[j];
- i++;
- flag = true;
- break;
- }
- }
- if (!flag)
- {
- Node* tmp = new Node(cur);
- // node->children.push_back(tmp);
- i++;
- }
- }
- }
- };
- struct Word{
- string letters;
- string digits;
- Word(string str)
- {
- letters = str;
- transcript();
- }
- Word(){}
- void transcript()
- {
- for (int i = 0; i < letters.length(); i++)
- {
- char temp = letters[i];
- if (temp == 'I' || temp == 'J' || temp == '1')
- digits += '1';
- if (temp == 'A' || temp == 'B' || temp == 'C' || temp == '2')
- digits += '2';
- if (temp == 'D' || temp == 'E' || temp == 'F' || temp == '3')
- digits += '3';
- if (temp == 'G' || temp == 'H' || temp == '4')
- digits += '4';
- if (temp == 'K' || temp == 'L' || temp == '5')
- digits += '5';
- if (temp == 'M' || temp == 'N' || temp == '6')
- digits += '6';
- if (temp == 'P' || temp == 'R' || temp == 'S' || temp == '7')
- digits += '7';
- if (temp == 'T' || temp == 'U' || temp == 'V' || temp == '8')
- digits += '8';
- if (temp == 'W' || temp == 'X' || temp == 'Y' || temp == '9')
- digits += '9';
- if (temp == 'O' || temp == 'Q' || temp == 'Z' || temp == '0')
- digits += '0';
- }
- }
- friend bool operator <(Word const & a, Word const & b)
- {
- return a.digits < b.digits;
- }
- };
- vector<Word> dictionary;
- string number = "";
- int dictionaryLength;
- vector<int> counter;
- vector<string> words;
- BOR b;
- void inizialize(ifstream& in)
- {
- in >> number;
- in >> dictionaryLength;
- dictionary.resize(dictionaryLength);
- for (int i = 0; i < dictionaryLength && !in.eof(); i++)
- {
- string str;
- in >> str;
- b.add(str);
- dictionary[i] = Word(str);
- }
- counter.resize(number.length());
- fill(counter.begin(), counter.end(), 1e9);
- words.resize(number.length());
- fill(words.begin(), words.end(), "");
- }
- bool compare(string s1, string s2)
- {
- if (s2.length() < s1.length())
- return false;
- string a1 = s1.substr(0, s1.length());
- string a2 = s2.substr(0, s1.length());
- if (a1 == a2)
- return true;
- return false;
- }
- vector<Word>::iterator find(vector<Word>::iterator& start, vector<Word>::iterator& end, string str){
- vector<Word>::iterator it;
- vector<Word>::iterator result = dictionary.end();
- for (it = start; it != end; it++)
- {
- if ((*it).digits == str)
- {
- result = it;
- start = it;
- break;
- }
- }
- for (it; it != end && compare(str, (*it).digits); it++);
- end = it;
- return result;
- }
- vector<string> solve()
- {
- vector<Word>::iterator it;
- vector<Word>::iterator start;
- vector<Word>::iterator end;
- for (int i = number.size()-1; i >=0; i--)
- {
- start = dictionary.begin();
- end = dictionary.end();
- for (int j = 0; j <number.size() - i-1; j++)
- {
- string str = number.substr(i, j+1);
- if (words[i+j+1] != "")
- {
- it = find(start, end, str);
- if (it != dictionary.end())
- if (counter[i+j+1] + 1 < counter[i]){
- words[i] = (*it).letters;
- counter[i] = counter[i+j+1]+1;
- }
- }
- }
- it = find(start, end, number.substr(i, number.size()-i));
- if (it!=dictionary.end()){
- words[i] = (*it).letters;
- counter[i] = 1;
- }
- }
- return words;
- }
- void main(){
- ifstream in("input.txt");
- ofstream out("output.txt");
- inizialize(in);
- sort(dictionary.begin(), dictionary.end());
- vector<string> r = solve();
- string result = "";
- int c = 0;
- int k = 1;
- if (r[c] == "")
- out << "No solution";
- else{
- result = r[c];
- c += r[c].length();
- while (c <r.size())
- {
- if (r[c] != ""){
- result += " " + r[c];
- c += r[c].length();
- k++;
- }
- else c--;
- }
- out << k << endl;
- out << result;
- }
- in.close();
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement