rt,我这里提供一组数据:
4
ab
bc
cd
da
output:abcda
错误代码:
#include<bits/stdc++.h>
using namespace std;
const int N=130+10;
int n,m;
int G[N][N];
int d[N];
int S,cnt;
stack<int>ans;
void dfs(int u){
for(int v=1;v<=n;v++)
if(G[u][v]){
G[u][v]--;
G[v][u]--;
dfs(v);
}
ans.push(u);
return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>m;
n=N-10;
for(int i=1;i<=m;i++){
string s;
cin>>s;
G[s[0]][s[1]]++;
G[s[1]][s[0]]++;
d[s[0]]++;
d[s[1]]++;
}
for(int i=n;i>=1;i--)
if(d[i]%2==1){
cnt++;
S=i;
}
if(S==0){
for(int i=n;i>=1;i--)//顺序反了
if(d[i]){
S=i;
break;
}
}
if(cnt>2){
cout<<"No Solution";
return 0;
}
dfs(S);
if(ans.size()<m+1){
cout<<"No Solution";
return 0;
}
while(!ans.empty()){
cout<<(char)ans.top();
ans.pop();
}
return 0;
}
//output:dabcd
由此可见,请求加强数据。
若是自身理解错误,请指正!