Advertisement
artemgf

Телефонные номера

Jun 11th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _USE_MATH_DEFINES
  4. #include <iostream>
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <stdio.h>
  11. #include <cmath>
  12. #include <math.h>
  13. #include <queue>
  14. #include <stack>
  15. #include <climits>
  16. #include <deque>
  17. #include <ctime>
  18. #include <iomanip>
  19. #include <bitset>
  20. #include <unordered_map>
  21. #include <unordered_set>
  22.  
  23. using namespace std;
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef unsigned int ui;
  28.  
  29. #define mh() make_heap()
  30. #define poph() pop_heap()
  31. #define pushh() push_heap()
  32. #define sor(n) n.begin(), n.end()
  33. #define rsor(n) n.rbegin(), n.rend()
  34. #define mp make_pair
  35. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  36. #define p(T) pair<T,T>
  37. #define toch(x) cout.precision(x), cout.setf(ios::fixed)
  38. #define znac(l) abs(l)/l
  39. #define IOS ios::sync_with_stdio(false)
  40. #define IOSB cin.tie(0), cout.tie(0);
  41. const ll ok = ll(1e9 + 7);
  42.  
  43. void bfs(vector<vector<ll>>&graf, vector<bool> &used, vector<ll> &var, int v)
  44. {
  45.     queue<ll>next;
  46.     next.push(v);
  47.     while (!next.empty())
  48.     {
  49.         ll now = next.front();
  50.         next.pop();
  51.         for (int i = 0; i < graf[now].size(); i++)
  52.         {
  53.             if (!used[graf[now][i]])
  54.             {
  55.                 used[graf[now][i]] = 1;
  56.                 var[graf[now][i]] = now;
  57.                 next.push(graf[now][i]);
  58.             }
  59.         }
  60.     }
  61. }
  62. int main()
  63. {
  64.     IOSB;
  65.     IOS;
  66. #ifdef TheCompiler
  67.     files;
  68. #endif
  69.     short numer[26] = { 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 };
  70.     string num = "";
  71.     cin >> num;
  72.    
  73.     while (num != "-1")
  74.     {
  75.         vector<vector<ll>>graf(102);
  76.         vector<bool> used(102, 0);
  77.         ll n;
  78.         cin >> n;
  79.         vector<string>word;
  80.         map<ll, set<string>> dic;
  81.         map<string, string>old;
  82.         for (int i = 1; i <= n; i++)
  83.         {
  84.             string s;
  85.             string h = "";
  86.             cin >> s;
  87.             for (int i = 0; i < s.size(); i++)
  88.             {
  89.                 h += numer[s[i] - 'a'] + '0';
  90.             }
  91.             ll p = num.find(h);
  92.             if (p >= 0 && p <= num.size())
  93.             {
  94.                 for (int i = 0; i < num.size(); i++)
  95.                 {
  96.                     if (num.find(h, i) == i)
  97.                     {
  98.                         graf[i].push_back(i + h.size());
  99.                     }
  100.                 }
  101.                 old[h] = s;
  102.             }
  103.  
  104.         }
  105.         vector<ll> var(102, -1);
  106.         bfs(graf, used, var, 0);
  107.         ll mn = LLONG_MAX, pos = -1;
  108.         stack<string>ans;
  109.         if (var[num.size()] != -1)
  110.         {
  111.             ll i = num.size();
  112.             string h = "";
  113.             while (var[i] != -1)
  114.             {
  115.                 for (int j = var[i]; j <i; j++)
  116.                 {
  117.                     h += num[j];
  118.                 }
  119.                 ans.push(h);
  120.                 h = "";
  121.                 i = var[i];
  122.             }
  123.             while (!ans.empty())
  124.             {
  125.                 h = ans.top();
  126.                 ans.pop();
  127.                 cout << old[h] << " ";
  128.             }
  129.             cout << endl;
  130.         }
  131.         else
  132.             cout << "No solution." << endl;
  133.         cin >> num;
  134.     }
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement