题目描述:给定一个字符串说明各字母之间相连的情况,每个字母的距离为到与它相连的字母中的最大距离,对这些字母求一个序列,使得最大距离最小。
思路:全排列么枚举,求出每种情况的最大距离取最小值即可。比赛时脑残读错题意了,以为排列顺序只能按边走结果dfs了半天都推不出样例中的3。改了一个小时反应过来,又删了用全排列写,结果全排列忘记排序并且全排列格式错了,一直不出结果,无奈又改回dfs模拟全排列,匆忙写完看都没看离比赛结束还有一分半的时候交上1Y过了。其实是有点小郁闷的,要不是读错题这题也不会写了将近两个小时。
代码:下面附上全排列和dfs两个版本的代码,vj上显示全排列的时间更短。
全排列版本:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 30;
const int maxt = 1e6 + 10;
const int mod = 10000007;
const int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
int n, m, k;
map<char, int> mp;
map<int, char> mp2;
vector<int> g[maxn];
int d[maxn][maxn];
int num[maxn];
bool vis[maxn];
int total_num;
string str;
int order[maxn];
int ans_order[maxn];
int ans;
int total_len;
void init(){
int cnt = 0;
for(char ch = 'A'; ch <= 'Z'; ++ch){//每个大写字母对应一个ID值
mp[ch] = ++cnt;
}
char ch = 'A';
for(int i = 1; i <= 26; ++i){
mp2[i] = ch;
++ch;
}
}
int main(){
init();
while(cin >> str){
if(str == "#") break;
for(int i = 0; i < maxn; ++i) g[i].clear();
char cu, cv;
int u, v;
int len = str.size();
int cur;
int cnt = 1;
string tmp;
memset(d, -1, sizeof d);
for(int i = 0; i < len; ++i){
cu = str[i];
cur = str.find(';', i);
if(cur == -1) cur = len;
i += 2;
for(; i < cur; ++i){
cv = str[i];
g[mp[cu]].push_back(mp[cv]);//保存边
g[mp[cv]].push_back(mp[cu]);
d[mp[cu]][mp[cv]] = d[mp[cv]][mp[cu]] = 1;//标记两边相连
}
}
total_num = mp.size();
total_len = 0;
for(int i = 1; i <= total_num; ++i){
if(!g[i].empty())sort(g[i].begin(), g[i].end()), ++total_len;//对于每个点对与其相连的点排序
}
ans = inf;
cnt = 0;
for(int i = 1; i <= total_num; ++i){
if(!g[i].empty())
order[++cnt] = i;
}
do{
int tmp = 0, cur;
for(int i = 1; i <= total_len; ++i){
for(int j = total_len; j >= i; --j)
if(d[order[i]][order[j]] != -1){
tmp = tmp < j - i ? j - i : tmp;//找该序列中的最大距离
if(tmp >= ans) break;
}
}
if(tmp >= ans) continue;//保存最小值
else{
ans = tmp;
for(int i = 1; i <= total_len; ++i){
ans_order[i] = order[i];
}
}
}while(next_permutation(order + 1, order + 1 + total_len));//全排列枚举所有情况
for(int i = 1; i <= total_len; ++i){
printf("%c ", mp2[ans_order[i]]);
}
printf("-> %d\n", ans);
}
return 0;
}
DFS版本:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 30;
const int maxt = 1e6 + 10;
const int mod = 10000007;
const int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, -1, 1, -1, 1, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
int n, m, k;
map<char, int> mp;
map<int, char> mp2;
vector<int> g[maxn];
int d[maxn][maxn];
int num[maxn];
bool vis[maxn];
int total_num;
string str;
int order[maxn];
int ans_order[maxn];
int ans;
int total_len;
void init(){
int cnt = 0;
for(char ch = 'A'; ch <= 'Z'; ++ch){
mp[ch] = ++cnt;
}
char ch = 'A';
for(int i = 1; i <= 26; ++i){
mp2[i] = ch;
++ch;
}
}
void solve(int u, int cnt){
if(cnt > total_len) return;
if(cnt == total_len){
int tmp = 0, cur;
for(int i = 0; i < cnt; ++i){//找最大距离
for(int j = cnt - 1; j >= 0; --j)
if(d[order[i]][order[j]] != -1){
tmp = tmp < j - i ? j - i : tmp;
}
}
if(ans > tmp){//记录最大距离中的最小值及对应排列顺序
ans = tmp;
for(int i = 0; i < cnt; ++i){
ans_order[i] = order[i];
}
}
}
int v, l = g[u].size();
for(int i = 1; i <= total_num; ++i){
if(g[i].empty() || vis[i]) continue;
vis[i] = true;
order[cnt] = i;
solve(i, cnt + 1);
vis[i] = false;
}
}
int main(){
init();
while(cin >> str){
if(str == "#") break;
for(int i = 0; i < maxn; ++i) g[i].clear();
char cu, cv;
int u, v;
int len = str.size();
int cur;
int cnt = 1;
string tmp;
memset(d, -1, sizeof d);
for(int i = 0; i < len; ++i){
cu = str[i];
cur = str.find(';', i);
if(cur == -1) cur = len;
i += 2;
for(; i < cur; ++i){
cv = str[i];
g[mp[cu]].push_back(mp[cv]);
g[mp[cv]].push_back(mp[cu]);
d[mp[cu]][mp[cv]] = d[mp[cv]][mp[cu]] = 1;
}
}
total_num = mp.size();
total_len = 0;
for(int i = 1; i <= total_num; ++i){
if(!g[i].empty())sort(g[i].begin(), g[i].end()), ++total_len;
}
ans = inf;
for(int i = 1; i < total_num; ++i){//以每个点(即每个字母)为起点枚举排列顺序。
if(g[i].empty()) continue;
memset(vis, 0, sizeof vis);
memset(order, 0, sizeof order);
vis[i] = true;
order[0] = i;
solve(i, 1);
}
for(int i = 0; i < total_len; ++i){
printf("%c ", mp2[ans_order[i]]);
}
printf("-> %d\n", ans);
}
return 0;
}