版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/81813957
#include<bits/stdc++.h>
using namespace std;
const int maxn = 8;
const int maxl = 90 + 5;
int L,snt,cnt,w;
char s[maxl];
int seq[maxn],vis[maxn];
int G[maxn][maxn];
int fact(int n){
if(n) return n*fact(n-1);
return 1;
}
void dfs(int cur,int wt){
while(cur == n && cnt = snt){
for(int i = 0;i < L;i++) printf("%c ",'A' + i);
printf("-> %d",w);
}
if(cur < n){
for(int i = 0;i < L;i++){
if(vis[i]) continue;
seq[cur] = i;
if(!cur) dfs(1,wt);
eles{
int ok = 1;
for(int j = 0;j < cur;j++){
if(G[seq[cur]][seq[j]]){
wt = min(wt,cur - j);
if(wt >= w){ ok = 0; break; }
}
}
if(ok) dfs(cur+1,wt);
else cnt += fact(L-cur-1);
}
}
}
int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
while(scanf("%s",s) && s[0] != '#'){
int len = strlen(s);
L = -1;
for(int i = 0;i < len;i++){
int j = s[i++] - 'A';
L = max(L,j);
while(isalpha(s[++i])){
int t = s[i] - 'A';
L = max(L,t);
G[j][t] = 1;
G[t][j] = 1;
}
}
cnt = 0;
L++;
snt = fact(L);
w = 100;
dfs(0,100);
}
return 0;
}