Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <string>
- #define wejscie first
- #define wyjscie second
- using namespace std;
- const int m=1000005;
- int odwiedzone[m];
- string t,pom,pom1;
- vector<pair<int,char> >tab[m];
- int tablica[m];
- int costam=0,ilestam=0;
- vector<char>wynik;
- pair<int,int>stopien[m];
- int converter(string tab){
- int index=int(tab[0])*1000+int(tab[1]);
- return index;
- }
- string zamiana(int tab){
- string ad="";
- ad+=char(tab/1000);
- ad+=char(tab%1000);
- return ad;
- }
- void dfs(int v,char c){
- while(!tab[v].empty()){
- costam++;
- int w=tab[v].back().first;
- char f=tab[v].back().second;
- tab[v].pop_back();
- dfs(w,f);
- }
- wynik.push_back(c);
- }
- int con(string tab){
- int wynik=tab[0]*10+tab[1]*5+tab[2]*7;
- return wynik;
- }
- int main(){
- int n;
- scanf("%d",&n);
- int ile=0;
- for(int i=0;i<n;++i){
- cin>>t;
- if(odwiedzone[con(t)]==0)ilestam++;
- odwiedzone[con(t)]=1;
- pom="";
- pom1="";
- pom+=t[0];
- pom+=t[1];
- pom1+=t[1];
- pom1+=t[2];
- int a=0,b=0;
- a=(converter(pom));
- b=(converter(pom1));
- tab[a].push_back(make_pair(b,t[2]));
- stopien[a].wyjscie++;
- stopien[b].wejscie++;
- }
- int czy=0,czy1=0;
- int start=-1,start1=-1;
- for(int i=0;i<=1000000;++i){
- if(stopien[i].wejscie==stopien[i].wyjscie&&stopien[i].wejscie>0){
- start1=i;
- ile++;
- }
- else if(stopien[i].wyjscie-stopien[i].wejscie==1){
- start=i;
- czy1++;
- }
- else if(stopien[i].wejscie-stopien[i].wyjscie==1){
- czy++;
- }
- }
- if(czy1==1&&czy==1&&start!=-1){
- string pom=zamiana(start);
- dfs(start,start);
- if(costam!=n||(wynik.size()+1)!=(n+2)){
- printf("-1\n");
- }
- else {
- printf("%c%c",pom[0],pom[1]);
- for(int i=wynik.size()-2;i>=0;--i){
- printf("%c",wynik[i]);
- }
- }
- }
- else if(czy==0&&czy1==0&&start1!=-1){
- string pom=zamiana(start1);
- dfs(start1,start1);
- if(costam!=n||(wynik.size()+1)!=(n+2)){
- printf("-1\n");
- }
- else {
- printf("%c%c",pom[0],pom[1]);
- for(int i=wynik.size()-2;i>=0;--i){
- printf("%c",wynik[i]);
- }
- }
- }
- else {
- printf("-1\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement