P1341 unordered letter pairs, P1341 letters

Source: Internet
Author: User

P1341 unordered letter pairs, P1341 letters
Description

Given n unordered letter pairs (case-sensitive, unordered, that is, the two letters in the letter pair can be reversed ). Create a string with n + 1 letters so that each letter pair appears in the string.

Input/Output Format Input Format:

Enter a positive integer n in the first line.

The following n rows contain two letters in each row, indicating that the two letters must be adjacent.

Output Format:

Output a string that meets the requirements.

If No string meets the requirements, output "No Solution ".

If there are multiple solutions, please output the solution with the smallest possible ASCII code (minimum Lexicographic Order) of the first letter

Input and Output sample Input example #1:
4aZtZXtaX
Output sample #1:
XaZtX 
Description

[Data scale and Conventions]

Different unordered letters have a limited number of pairs. The n size can be calculated.

 

 

This is the bare question of Euler's loop.

But there are two points to note:

1. output in a function cannot use a value-passing variable as a cyclic variable.

2. ios: sync may cause RE! ,

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 using namespace std; 7 const int MAXN=4001; 8 void read(int & n) 9 {10     char c='+';int x=0;int flag=0;11     while(c<'0'||c>'9')12     {    c=getchar();    if(c=='-')    flag=1;    }13     while(c>='0'&&c<='9')14     {x=x*10+(c-48);c=getchar();}15     flag==1?n=-x:n=x;16 }17 int n; 18 int map[MAXN][MAXN];19 int indegree[MAXN];20 int ans[MAXN];21 int flag=0;22 void dfs(int num,int now)23 {24     ans[now]=num;25     if(now==n+1)26     {27         for(int i=1;i<=n+1;i++)28         printf("%c",(char)ans[i]);29         exit(0);30     }31     for(int i=65;i<=127;i++)32     {33         if(map[num][i])34         {35             map[num][i]=0;36             map[i][num]=0;37             dfs(i,now+1);38             map[num][i]=1;39             map[i][num]=1;40         }41     }42     ans[now]=0;43 }44 int main()45 {46     cin>>n;47     ios::sync_with_stdio(false);48     for(int i=1;i<=n;i++)49     {50         char a,b;51         cin>>a>>b;52         if(!map[a][b])53         {54             map[a][b]=1;55             map[b][a]=1;56             indegree[a]++;57             indegree[b]++;58         }59     }60     int tot=0;61     int bgji=0x7fff,bgou=0x7ffff;62     for(int i=65;i<=127;i++)63     {64         if(indegree[i]%2==1)65         {66             tot++;67             bgji=min(bgji,i);68         }69         else if(indegree[i])70         bgou=min(i,bgou);71     }72     if(tot!=0&&tot!=2)73     {74         printf("No Solution");75         exit(0);76     }77     tot==0?78     dfs(bgou,1):79     dfs(bgji,1);80     return 0;81 }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.