Advertisement
osipyonok

SP 2 cpp

Sep 22nd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define INF 1000010000
  4. #define nl '\n'
  5. #define pb push_back
  6. #define ppb pop_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define pii pair<int,int>
  11. #define pdd pair<double,double>
  12. #define all(c) (c).begin(), (c).end()
  13. #define SORT(c) sort(all(c))
  14. #define sz(c) (c).size()
  15. #define rep(i,n) for( int i = 0; i < n; ++i )
  16. #define repi(i,n) for( int i = 1 ; i <= n; ++i )
  17. #define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
  18. #define repf(j,i,n) for( int j = i ; j < n ; ++j )
  19. #define die(s) {std::cout << s << nl;}
  20. #define dier(s) {std::cout << s; return 0;}
  21. #define vi vector<int>
  22. typedef long long ll;
  23.  
  24. using namespace std;
  25.  
  26. template <typename T>
  27. class Automata{
  28.     public:
  29.         int A;
  30.         T s;
  31.         vector<T> S;
  32.         vector<T> F;
  33.         map<pair<T , char> , T> f;
  34.         map<pair<T , T> , char> inverse_f;
  35.    
  36.     Automata(int _A , vector<T> _S , T _s  , vector<T> _F , map<pair<T , char> , T >  _f){
  37.         A = _A;
  38.         s = _s;
  39.         S = _S;
  40.         F = _F;
  41.         f = _f;
  42.         for(typename map<pair<T , char> , T >::iterator it = f.begin() ; it != f.end(); ++it)
  43.             inverse_f[{it->fi.fi , it->se}] = it->fi.se;
  44.     //  cout << "f(" << it->fi.fi << " , " << it->fi.se << ") = " << it->second << nl;
  45.        
  46.     }
  47.    
  48.     private: T bfs(map<T , int> &d , map<T , T> & p , map<T , bool> & used){
  49.         queue<T> q;
  50.         q.push(s);
  51.         used[s] = 1;
  52.         p[s] = NULL;
  53.         T ret = NULL;
  54.         while(sz(q)){
  55.             T v = q.front();
  56.             q.pop();
  57.             for(char c = 'a' ; c < A + 'a' ; ++c){
  58.                 if(f.find({v , c}) != f.end()){
  59.                     T to = f[{v , c}];
  60.                     if(!used[to]){
  61.                         used[to] = 1;
  62.                         q.push(to);
  63.                         d[to] = d[v] + 1;
  64.                         p[to] = v;
  65.                         if(find(F.begin(), F.end(), to)!= F.end()){
  66.                             ret = to;
  67.                             cout << "We have found a finish vertex!" << nl;
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.         return ret;
  74.     }
  75.    
  76.     public: vector<T> Solve_10(){
  77.         map<T , int> d;
  78.         map<T , T> p;
  79.         map<T , bool> used;
  80.         T need = bfs(d , p , used);
  81.         vector<T> path;
  82.         if (need == NULL)
  83.             cout << "No word!";
  84.         else{
  85.             for (T v=need; v!=NULL; v=p[v])
  86.                 path.push_back (v);
  87.             path.pb(s);
  88.             reverse (path.begin(), path.end());
  89.         /*  cout << "Word: ";
  90.             for (size_t i=0; i<path.size()-1; ++i)
  91.                 cout << a1.inverse_f[{path[i] , path[i + 1]}] << " ";*/
  92.         }
  93.         return path;
  94.     }
  95.    
  96.     public: Automata<pair<T , T> > Multiply (Automata<T> a , Automata<T> b){
  97.         int new_A = max(a.A , b.A);
  98.         vector<pair<T , T> > new_S;
  99.         for(int i = 0 ; i < sz(a.S) ; ++i){
  100.             for(int j = 0 ; j < sz(b.S) ; ++j){
  101.                 new_S.pb({a.S[i] , b.S[j]});
  102.             }
  103.         }
  104.     /*  for(T a.S: i){
  105.             for(T b.S : j){
  106.                 new_S.pb({i , j});
  107.             }
  108.         }*/
  109.         pair<T , T> new_s = {a.s , b.s};
  110.         vector<pair<T , T> > new_F;
  111.         for(int i = 0 ; i < sz(a.F) ; ++i){
  112.             for(int j = 0 ; j < sz(b.F) ; ++j){
  113.                 new_S.pb({a.F[i] , b.F[j]});
  114.             }
  115.         }
  116.         map<pair<pair<T , T> , char> , pair<T , T> > new_f;
  117.        
  118.         for(char c = 'a' ; c < 'a' + new_A ; ++c){
  119.             for(pair<T , T> i : new_S){
  120.                 if(a.f.find({i.fi , c}) != a.f.end() && b.f.find({i.se , c}) != b.f.end()){
  121.                     new_f[{i , c}] = { a.f[{i.fi , c}] , b.f[{i.se , c}] };
  122.                 }
  123.             }
  124.         }
  125.         Automata<pair<T , T> > res = Automata<pair<T , T> >(new_A , new_S , new_s , new_F , new_f);
  126.         return res;
  127.     }
  128.  
  129. };
  130.    
  131. int main() {
  132.     ios_base::sync_with_stdio(false);
  133.     cin.tie(NULL);
  134.     cout.precision(0);
  135.     int A;
  136.     int S1;
  137.     int s;
  138.     cin >> A >> S1 >> s;
  139.     vi S(S1 , 0);
  140.     rep(i , S1)S[i] = i;
  141.     int t;
  142.     cin >> t;
  143.     vi F(t , 0);
  144.     rep(i , t)cin >> F[i];
  145.     map<pair<int , char> , int>  f;
  146.     cin >> t;
  147.     rep(i , t){
  148.         int u , v;
  149.         char a;
  150.         cin >> u >> a >> v;
  151.         f[{u , a}] = v;
  152.     }
  153.     Automata<int> a1 = Automata<int>(A , S , s , F , f);
  154.    
  155.     for(char ch = 'a' ; ch < 'a' + a1.A ; ++ch)cout  << ch << " ";
  156.     cout << nl;
  157.     for(int i : a1.S)cout << i << " ";
  158.     cout << nl;
  159.     cout << a1.s << nl;
  160.     for(int i : a1.F)cout << i << " ";
  161.     cout << nl;
  162.     for (map<pair<int , char> , int>::iterator it = a1.f.begin() ; it != a1.f.end(); ++it)
  163.         cout << "f(" << it->fi.fi << " , " << it->fi.se << ") = " << it->second << nl;
  164.    
  165. /*  queue<int> q;
  166.     q.push(s);
  167. //  vector<bool> used(1000 , 0);
  168.     map<int , bool> used;
  169.     used[s] = 1;
  170. //  vi d(1000) , p(1000);
  171.     map<int , int> d , p;
  172.     p[s] = -1;
  173.     int final = -1;
  174.     while(sz(q)){
  175.         int v = q.front();
  176.         q.pop();
  177.         for(char c = 'a' ; c < a1.A + 'a' ; ++c){
  178.             if(a1.f.find({v , c}) != a1.f.end()){
  179.                 int to = a1.f[{v , c}];
  180.                 if(!used[to]){
  181.                     used[to] = true;
  182.                     q.push(to);
  183.                     d[to] = d[v] + 1;
  184.                     p[to] = v;
  185.                     if(find(a1.F.begin(), a1.F.end(), to)!= a1.F.end()){
  186.                         final = to;
  187.                         cout << "We have found a finish vertex!" << nl;
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193.     cout << d[final] << nl;
  194.     if (!used[final])
  195.         cout << "No word!";
  196.     else{
  197.         vector<int> path;
  198.         for (int v=final; v!=-1; v=p[v])
  199.             path.push_back (v);
  200.         reverse (path.begin(), path.end());
  201.         cout << "Word: ";
  202.         for (size_t i=0; i<path.size()-1; ++i)
  203.             cout << a1.inverse_f[{path[i] , path[i + 1]}] << " ";
  204.     }*/
  205.     vector<int> path= a1.Solve_10();
  206.     for (size_t i=0; i<path.size()-1; ++i)
  207.             cout << a1.inverse_f[{path[i] , path[i + 1]}] << " ";
  208.     return 0;
  209. }
  210. /*
  211. 2
  212. 3
  213. 0
  214.  
  215. 1 2
  216. 4
  217. 0 a 1
  218. 1 a 2
  219. 0 b 2
  220. 1 b 2
  221.  
  222.  
  223. 3
  224. 6
  225. 0
  226.  
  227. 1 5
  228. 9
  229. 0 b 2
  230. 0 a 1
  231. 1 b 3
  232. 2 a 3
  233. 3 a 4
  234. 3 b 0
  235. 4 a 2
  236. 4 c 5
  237. 4 b 1
  238. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement