就是一道 DFS剪枝+枚举全排列 的题目
我用了vector来存点对的,不用考虑越界的问题。不过效率貌似不如直接用数组高。
WA了三次,花了好长时间找原因╮(╯▽╰)╭
多么弱智的错误啊=。=
WA代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <math.h>
#include <map>
#include <set>
using namespace std;
vector<vector<int> > align;
vector<int> dict;
set<int> s;
int n;
int A[27];
int B[27];
int ans;
void parse(string cmd){
int len = cmd.length();
int f = 1;
int x = -1;
for(int i=0;i<len;i++){
if(cmd[i]>=‘A‘ && cmd[i] <=‘Z‘){
if(!s.count(cmd[i]-‘A‘)) {
dict.push_back(cmd[i]-‘A‘);s.insert(cmd[i]-‘A‘);}
}
if(cmd[i] == ‘ ‘) continue;
if(cmd[i] == ‘:‘) {f = 0;continue;}
if(cmd[i] == ‘;‘) {f = 1;continue;}
if(f==1) x = cmd[i] - ‘A‘;
vector<int> tmp;
if(f==0){
tmp.push_back(x);tmp.push_back(cmd[i]-‘A‘);
align.push_back(tmp);
}
}
}
void dfs(int cur,int dist){
if(cur==n ){
if(dist < ans) {
ans = dist;
for(int i=0;i<n;i++) B[i] = A[i];
}
return;
}
//else if(dist > ans) return;
else for(int i=0;i<n;i++){
int ok = 1;
//这里的循环不能在一起写,否则就会出现 A B B ** 也符合的情况,但这样竟然能过样例==
for(int j = 0;j<cur;j++){
if(A[j] == dict[i]) {ok = 0;break;}
for(int k=0;k<align.size();k++){
if((align[k][0] == dict[i] && align[k][1]==A[j])||(align[k][0]==A[j] && align[k][1]==dict[i])){
if(cur-j>=ans){ok=0;break;
if(cur-j>dist) dist = cur-j;
}
}
if(!ok) break;
}
if(ok){
A[cur] = dict[i];
dfs(cur+1,dist);
}
}
}
int main(){
string line;
while(cin>>line){
align.clear();dict.clear();s.clear();
ans = 100000000;
if(line[0]==‘#‘) break;
parse(line);
n = dict.size();
sort(dict.begin(),dict.end());
dfs(0,0);
for(int i=0;i<n;i++){
printf("%c ",‘A‘+B[i]);
}
printf("-> %d\n",ans);
}
return 0;
}
AC代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <math.h>
#include <map>
#include <set>
using namespace std;
vector<vector<int> > align;
vector<int> dict;
set<int> s;
int n;
int A[270];
int B[270];
int ans;
void parse(string cmd){
int len = cmd.length();
int f = 1;
int x = -1;
for(int i=0;i<len;i++){
if(cmd[i]>=‘A‘ && cmd[i] <=‘Z‘){
if(!s.count(cmd[i]-‘A‘)) {
dict.push_back(cmd[i]-‘A‘);s.insert(cmd[i]-‘A‘);}
}
if(cmd[i] == ‘ ‘) continue;
if(cmd[i] == ‘:‘) {f = 0;continue;}
if(cmd[i] == ‘;‘) {f = 1;continue;}
if(f==1) x = cmd[i] - ‘A‘;
vector<int> tmp;
if(f==0){
tmp.push_back(x);tmp.push_back(cmd[i]-‘A‘);
align.push_back(tmp);
}
}
}
void dfs(int cur,int dist){
if(cur==n ){
if(dist < ans) {
ans = dist;
for(int i=0;i<n;i++) B[i] = A[i];
/*printf("ans:%d\n",ans);
for(int i=0;i<n;i++) printf("%c ",A[i]+‘A‘);
printf("\n");*/
}
return;
}
//else if(dist > ans) return;
else for(int i=0;i<n;i++){
int ok = 1;
for(int j = 0;j<cur;j++){
if(A[j] == dict[i]) {ok = 0;break;}
}
if(ok){
for(int j = 0;j<cur;j++){
for(int k=0;k<align.size();k++){
if((align[k][0] == dict[i] && align[k][1]==A[j])||(align[k][0]==A[j] && align[k][1]==dict[i])){
if(cur-j>=ans){ok=0;break;}
/*
for(int m =0;m<cur;m++) printf("%c ",A[m]+‘A‘);
printf("\nnow\n");
printf("%c %c\n",A[j]+‘A‘,dict[i]+‘A‘);*/
//cout<<dict[i]<<" "<<A[j]<<" "<<cur-j<<endl;
if(cur-j>dist){dist = cur-j;
/*printf("now\n");
printf("cur:%d i:%d j:%d %c %c ans:%d\n",cur,i,j,A[j]+‘A‘,dict[i]+‘A‘,dist);*/
break;}
}
}
if(!ok) break;
}
}
if(ok){
A[cur] = dict[i];
dfs(cur+1,dist);
}
}
}
int main(){
string line;
while(cin>>line){
align.clear();dict.clear();s.clear();
ans = 999999;
if(line[0]==‘#‘) break;
parse(line);
n = dict.size();
for(int i=0;i<n;i++) ;
sort(dict.begin(),dict.end());
dfs(0,0);
for(int i=0;i<n;i++){
printf("%c ",‘A‘+B[i]);
}
printf("-> %d\n",ans);
}
return 0;
}
原文地址:http://blog.csdn.net/iboxty/article/details/46319111