Advertisement
jedrzejd

Untitled

Sep 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cmath>
  6. #include <string>
  7. #define wejscie first
  8. #define wyjscie second
  9. using namespace std;
  10. const int m=1000005;
  11. int odwiedzone[m];
  12. string t,pom,pom1;
  13. vector<pair<int,char> >tab[m];
  14. int tablica[m];
  15. int costam=0,ilestam=0;
  16. vector<char>wynik;
  17. pair<int,int>stopien[m];
  18. int converter(string tab){
  19.     int index=int(tab[0])*1000+int(tab[1]);
  20.     return index;
  21. }
  22. string zamiana(int tab){
  23.     string ad="";
  24.     ad+=char(tab/1000);
  25.     ad+=char(tab%1000);
  26.     return ad;
  27. }
  28. void dfs(int v,char c){
  29.     while(!tab[v].empty()){
  30.         costam++;
  31.         int w=tab[v].back().first;
  32.         char f=tab[v].back().second;
  33.         tab[v].pop_back();
  34.         dfs(w,f);
  35.     }
  36.     wynik.push_back(c);
  37. }
  38. int con(string tab){
  39.     int wynik=tab[0]*10+tab[1]*5+tab[2]*7;
  40.     return wynik;
  41. }
  42. int main(){
  43.     int n;
  44.     scanf("%d",&n);
  45.     int ile=0;
  46.     for(int i=0;i<n;++i){
  47.         cin>>t;
  48.         if(odwiedzone[con(t)]==0)ilestam++;
  49.         odwiedzone[con(t)]=1;
  50.         pom="";
  51.         pom1="";
  52.         pom+=t[0];
  53.         pom+=t[1];
  54.         pom1+=t[1];
  55.         pom1+=t[2];
  56.         int a=0,b=0;
  57.         a=(converter(pom));
  58.         b=(converter(pom1));
  59.         tab[a].push_back(make_pair(b,t[2]));
  60.         stopien[a].wyjscie++;
  61.         stopien[b].wejscie++;
  62.     }
  63.     int czy=0,czy1=0;
  64.     int start=-1,start1=-1;
  65.     for(int i=0;i<=1000000;++i){
  66.         if(stopien[i].wejscie==stopien[i].wyjscie&&stopien[i].wejscie>0){
  67.             start1=i;
  68.             ile++;
  69.         }
  70.         else if(stopien[i].wyjscie-stopien[i].wejscie==1){
  71.             start=i;
  72.             czy1++;
  73.         }
  74.         else if(stopien[i].wejscie-stopien[i].wyjscie==1){
  75.             czy++;
  76.         }
  77.     }
  78.     if(czy1==1&&czy==1&&start!=-1){
  79.         string pom=zamiana(start);
  80.         dfs(start,start);
  81.         if(costam!=n||(wynik.size()+1)!=(n+2)){
  82.             printf("-1\n");
  83.         }
  84.         else {
  85.             printf("%c%c",pom[0],pom[1]);
  86.             for(int i=wynik.size()-2;i>=0;--i){
  87.                 printf("%c",wynik[i]);
  88.             }
  89.         }
  90.     }
  91.     else if(czy==0&&czy1==0&&start1!=-1){
  92.         string pom=zamiana(start1);
  93.         dfs(start1,start1);
  94.         if(costam!=n||(wynik.size()+1)!=(n+2)){
  95.             printf("-1\n");
  96.         }
  97.         else {
  98.             printf("%c%c",pom[0],pom[1]);
  99.             for(int i=wynik.size()-2;i>=0;--i){
  100.                 printf("%c",wynik[i]);
  101.             }
  102.         }
  103.     }
  104.     else {
  105.         printf("-1\n");
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement