#include <iostream> #include <conio.h> #include <string> #include <stack> #include <vector> using namespace std; const char b[] = {'2', '2', '2', '3', '3', '3', '4', '4', '1', '1', '5', '5', '6', '6', '0', '7', '0', '7', '7', '8', '8', '8', '9', '9', '9', '0'}; string toDigits(const string& s) { string _tmp; for(int i=0; i<s.length(); ++i) { char c = s[i]; _tmp += b[c-97]; } return _tmp; } int main() { // string s = "7325188087"; // char *dict_s[] = {"it", "your", "reality", "real", "tour"}; // string dict_i[] = {"18", "9087", "7325189", "7325", "8087"}; // int dict_len[] = {2, 4, 7, 4, 4}; for(;;) { string s; // number cin >> s; if(s == "-1") break; int dsize(0); // dictionary size cin >> dsize; vector<string> dict_s, dict_i; vector<int> dict_len; for(int i=0; i< dsize; i++) { string _tmp; cin >> _tmp; dict_s.push_back(_tmp); } for(int i=0; i<dict_s.size(); i++) { string _tmp = toDigits(dict_s[i]); dict_i.push_back(_tmp); dict_len.push_back(_tmp.length()); } bool checked[101]; int no_words[101], idx_words[101]; for(int i=0; i<=100; i++) { checked[i] = false; no_words[i] = 101; } checked[1] = true; no_words[0] = 0; for(int i=1; i <= s.length(); ++i) { if(checked[i]) { for(int j=0; j<dsize; j++) // j varies from 0 to size of dict_i { unsigned len = dict_len[j]; unsigned l = s.substr(i-1, len).find(dict_i[j]); if(l != -1) { //printf("%s found at %d enabling %d\\n", dict_s[j], i, i+len); checked[i+len] = true; if(no_words[i-1]+1 < no_words[i+len-1]) { //printf("#words %d < #words %d at %d\\n", no_words[i-1]+1, no_words[i+len-1], i+len-1); no_words[i+len-1] = no_words[i-1]+1; idx_words[i+len-1] = j; } } } } } int idx = s.length(); if(no_words[idx] != 101) { stack<string> op; while(idx>0) { op.push(dict_s[idx_words[idx]]); idx -= dict_len[idx_words[idx]]; } while(!op.empty()) { cout << op.top() << " "; op.pop(); } } else cout << "No solution."; cout << endl; } }
Yet another blog from yet another software engineer - a collection of my thoughts and some snippets of code I write (mainly for my later reference). If you find this useful, lets discuss in comments.
November 14, 2013
TIMUS 1002 - Using Dynamic Programming
Problem: http://acm.timus.ru/problem.aspx?space=1&num=1002
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment