UVALive 7352 Dance Recital

时间:2024-07-17 13:04:38

题意:

有n种舞蹈要跳 每种舞蹈需要每一行字符串所对应的那些人

如果一个人连着跳两个舞蹈 那么需要一个quick change

问需要的最少quick changes是多少

思路:

假期的题 又拿出来做一遍……

范围很感人10*26 这不暴力搜一发还等啥呢……

 /* ***********************************************
Author :Sun Yuefeng
Created Time :2016/10/16 20:17:34
File Name :A.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f; int n,sum,ans; char dance[][maxn];
int match[maxn][maxn];
int way[];
bool vis[maxn]; int solve(int x,int y){ //看两个舞蹈有几个人重复了
int sum=;
int lenx=strlen(dance[x]);
int leny=strlen(dance[y]);
for(int i=;i<lenx;i++)
for(int j=;j<leny;j++)
sum+=(dance[x][i]==dance[y][j]);
return sum;
} void dfs(int x){ //强行遍历
if(x>n){
if(sum<ans) ans=sum;
return ;
}
for(int i=;i<=n;i++){ //遍历部分
if(!vis[i]){
way[x]=i;
vis[i]=true;
sum+=match[way[x-]][i];
dfs(x+);
sum-=match[way[x-]][i];
vis[i]=false;
}
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n)){
getchar();
M(vis,false),M(match,),M(way,);
for(int i=;i<=n;i++)
scanf("%s",dance[i]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(i!=j)
match[i][j]=match[j][i]=solve(i,j); //预处理匹配状况
}
}
ans=inf; //初始化ans sum
sum=;
dfs(); //搜索
printf("%d\n",ans); }
return ;
}
/* 5
ABC
ABEF
DEF
ABCDE
FGH
6
BDE
FGH
DEF
ABC
BDE
ABEF
4
XYZ
XYZ
ABYZ
Z */