给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒)。请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现。
第一行输入一个正整数n。
以下n行每行两个字母,表示这两个字母需要相邻。
输出格式:
输出满足要求的字符串。
如果没有满足要求的字符串,请输出“No Solution”。
如果有多种方案,请输出前面的字母的ASCII编码尽可能小的(字典序最小)的方案
【数据规模与约定】
不同的无序字母对个数有限,n的规模可以通过计算得到。
题解:刚读完题目没有什么思路,想打个爆搜搞一搞。然而回头一想这是图论专题于是画了个图,将每对字母连起来,每个字母都要出现而且要n+1个字母,就是要把所有边不重复走的情况下遍历一边。好像叫什么...欧拉路!对没错欧拉路。水题啊开心。然而实现的时候有些蜜汁错误搞不懂
const maxn=100000; var a:array['A'..'z','A'..'z']of longint; kk:array['A'..'z','A'..'z']of boolean; du,v:array['A'..'z']of longint; ans:array[0..maxn]of char; n,sum,i,tot:longint; fi,ch:char; procedure init; var i:longint; ch1,ch2,p1,p2:char; begin readln(n);p1:='0';p2:='0';fi:='0'; for i:=1 to n do begin readln(ch1,ch2); inc(a[ch1,ch2]); inc(a[ch2,ch1]); inc(du[ch1]);inc(du[ch2]); // kk[ch1,ch2]:=true;kk[ch2,ch1]:=true; end; tot:=0; for ch1:='A' to 'z' do begin if (du[ch1]>0)and(p2='0') then p2:=ch1; if odd(du[ch1]) then begin inc(tot); if p1='0' then p1:=ch1; end; end; if tot=2 then fi:=p1; if tot=0 then fi:=p2; end; procedure dfs(dep:longint;x:char); var ch:char; begin ans[dep]:=x; if dep=n+1 then begin for i:=1 to n+1 do write(ans[i]); halt; end; for ch:='A' to 'z' do if (a[x,ch]>0) then begin dec(a[ch,x]);dec(a[x,ch]); dfs(dep+1,ch); inc(a[ch,x]);inc(a[x,ch]); end; end; begin init; if (tot<>0)and(tot<>2) then begin writeln('No Solution'); halt; end; dfs(1,fi); for i:=1 to sum do write(ans[i]); end.
