Advertisement
cmiN

329 div2 III

Mar 19th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <sstream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8.  
  9. struct ProbabilisticTranslator {
  10.  
  11.     typedef vector<pair<string, int> > vp_t;
  12.     typedef vector<string> vs_t;
  13.     typedef map<string, vector<string> > msv_t;
  14.     typedef map<pair<string, string>, int> mpi_t;
  15.  
  16.     int maxValue;
  17.     vs_t text; // contiguous
  18.     msv_t dict;
  19.     mpi_t freq;
  20.     vp_t vec; // vector used to calculate `maxValue`
  21.  
  22.     void dp()
  23.     {
  24.         maxValue = 0;
  25.         msv_t::iterator it = dict.find(text[0]);
  26.         for (int i = 0; i < it->second.size(); ++i) vec.push_back(make_pair(it->second[i], 0));
  27.         for (int i = 1; i < text.size(); ++i) {
  28.             vp_t tmpVec;
  29.             it = dict.find(text[i]); // find possible translation
  30.             for (int j = 0; j < it->second.size(); ++j) { // for each of them
  31.                 int localMax = 0;
  32.                 for (int k = 0; k < vec.size(); ++k) {
  33.                     mpi_t::iterator freqIt = freq.find(make_pair(vec[k].first, it->second[j]));
  34.                     int newValue = freqIt == freq.end() ? 0 : freqIt->second;
  35.                     newValue += vec[k].second;
  36.                     localMax = max(localMax, newValue);
  37.                 }
  38.                 maxValue = max(maxValue, localMax);
  39.                 tmpVec.push_back(make_pair(it->second[j], localMax));
  40.             }
  41.             vec = tmpVec;
  42.         }
  43.     }
  44.  
  45.     int maximumFidelity(vs_t text, vs_t dict, vs_t freq)
  46.     {
  47.         // parse data
  48.         for (int i = 0; i < text.size(); ++i) {
  49.             stringstream sio;
  50.             string word;
  51.             sio << text[i];
  52.             while (sio >> word) this->text.push_back(word);
  53.         }
  54.         for (int i = 0; i < dict.size(); ++i) {
  55.             stringstream sio;
  56.             string word, value;
  57.             sio << dict[i];
  58.             sio >> word >> value;
  59.             while (sio >> value) this->dict[word].push_back(value);
  60.         }
  61.         for (int i = 0; i < freq.size(); ++i) {
  62.             stringstream sio;
  63.             string first, second;
  64.             int coef;
  65.             sio << freq[i];
  66.             sio >> first >> second >> coef;
  67.             this->freq[make_pair(first, second)] = coef;
  68.         }
  69.         // solve
  70.         dp();
  71.         return maxValue;
  72.     }
  73. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement