Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. vector<pair<int, int>> graph;
  8. /*
  9.  * E -> C
  10.  * costs(v, u) = c
  11.  * c[i] - сколько получим, поставив i-ую букву на это ребро
  12.  */
  13. map<pair<int, int>, vector<double>> costs;
  14.  
  15. char get_opt(pair<int, int> edge)
  16. {
  17.     double max = -1.0;
  18.     char opt = 'a';
  19.     vector<double> counts = costs[edge];
  20.     for (int i = 0; i <= 'z' - 'a'; i++)
  21.     {
  22.         if (counts[i] >= max)
  23.         {
  24.             max = counts[i];
  25.             opt = static_cast<char>('a' + i);
  26.         }
  27.     }
  28.     return opt;
  29. }
  30.  
  31. int main()
  32. {
  33.     freopen("discrete.in", "r", stdin);
  34.     freopen("discrete.out", "w", stdout);
  35.     int n;
  36.     int m;
  37.     cin >> n >> m;
  38.     for (int i = 0; i < n; i++)
  39.     {
  40.         int t0;
  41.         int t1;
  42.         cin >> t0 >> t1;
  43.         graph.emplace_back(t0 - 1, t1 - 1);
  44.         costs[{i, t0 - 1}] = vector<double>(31);
  45.         costs[{i, t1 - 1}] = vector<double>(31);
  46.  
  47.         for (int j = 0; j <= 'z' - 'a'; j++)
  48.         {
  49.             costs[{i, t0 - 1}][j] = 0.0;
  50.             costs[{i, t1 - 1}][j] = 0.0;
  51.         }
  52.     }
  53.     for (int i = 0; i < m; i++)
  54.     {
  55.         int len;
  56.         string input;
  57.         string output;
  58.         cin >> len >> input >> output;
  59.  
  60.         int cur_state = 0;
  61.  
  62.         for (int j = 0; j < len; j++)
  63.         {
  64.             char cur_input = input[j];
  65.             char cur_output = output[j];
  66.             int next_state = -1;
  67.             if (cur_input == '0')
  68.             {
  69.                 next_state = graph[cur_state].first;
  70.             }
  71.             else
  72.             {
  73.                 next_state = graph[cur_state].second;
  74.             }
  75.             pair<int, int> cur_edge = make_pair(cur_state, next_state);
  76.             costs[cur_edge][cur_output - 'a'] += (static_cast<double>(1) / static_cast<double>(len));
  77.             cur_state = next_state;
  78.         }
  79.     }
  80.  
  81.     for (int i = 0; i < n; i++)
  82.     {
  83.         int t0 = graph[i].first;
  84.         int t1 = graph[i].second;
  85.         cout << get_opt({i, t0}) << " " <<  get_opt({i, t1}) << endl;
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement