ACM Dance Recital(dfs+剪枝)
The Production Manager of a dance company has been tasked with determining the cost for the seasonal
dance recital. Because of their exceptional skills, many dancers will perform in more than one routine,
but this presents a problem; each dance routine incorporates a unique costume, so between routines,
dancers must report backstage to a Wardrobe Specialist, who can change the dancer’s costume in time
to begin their next scheduled routine.
A Wardrobe Specialist does a normal change on a dancer when the dancer performs in two routines
that are not consecutive, but when a dancer is required to perform in two consecutive routines, a quick
change is necessary. A Wardrobe Specialist charges a flat rate per recital that covers all normal changes,
but charges an exorbitant amount for each quick change. The Production Manager is responsible for
keeping the show under budget, and has hired you to write a program to report the minimum number
of quick changes needed for a given recital, given that the order of the dance routines could be changed.
To describe the cast of dancers that are to perform during a recital, each dancer is assigned an
identifying uppercase letter. (Fortunately, there are never more than 26 dancers, so characters from A
to Z suffice.) To describe a full recital, a list of individual routines is given, with a string of characters
defining which dancers appear in a routine. For example, consider the following recital description:
ABC
ABEF
DEF
ABCDE
FGH
The above list describes a recital with 5 dance routines, including a total of 8 individual performers
(dancers A through H). The first routine listed includes dancers {A, B, and C}. The second routine
includes dancers {A, B, E, and F}. Notice that if these first two routines are performed in the above
order, dancers A and B will require a quick change between the routines. In fact, if these five routines
are scheduled in the order given above, a total of six quick changes are required. However, the schedule
can be rearranged as follows:
ABEF
DEF
ABC
FGH
ABCDE
In this case, only two quick changes are required (those for E and F between the first two dances).
Input
The input file contains several test cases, each of them as described below.
The first line contains a single integer R, with 2 ≤ R ≤ 10, that indicates the number of routines
in the recital. Following that will be R additional lines, each describing the dancers for one routine in
the form of a nonempty string of up to 26 non-repeating, lexicographically sorted uppercase alphabetic
characters identifying the dancers who perform in that routine. Although a dancer’s letter will not
appear more than once in a single routine, that dancer may appear in many different routines, and it
may be that two or more routines have the identical set of dancers.ACM-ICPC Live Archive: 7352 – Dance Recital
2/2
Output
For each test case, output a single integer designating the minimum number of quick changes required
for the recital on a line by itself.
Sample Input
5
ABC
ABEF
DEF
ABCDE
FGH
6
BDE
FGH
DEF
ABC
BDE
ABEF
4
XYZ
XYZ
ABYZ
Z
Sample Output
2
3
4
题意:大概意思就是说给你n个字符数组,让你找出匹配度(相邻两个字符串之间相同元素的个数)最小的序列。
题解:由于n<=10,离线找出任意两个字符窜之间的匹配度,暴力dfs搜索+剪枝;如果在搜索的过程中sum>output(最小值,就不用继续搜了(剪枝);
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<string,string>pair1;
const int MAXN=1e3+;
int m,n,sum,output=MAXN;
int vis[MAXN];//标记数组
vector<string>str;
int ans[MAXN];//记录搜索的顺序
int mp[MAXN][MAXN];//第i个字符串和第j个字符串之间的匹配度
void ask_Qpoint()//求任意两个字符串之间的匹配度
{
for(int i=; i<m; i++)
{
for(int j=; j<m; j++)
{
int cnt=;
for(int k=,len=str[j].size(); k<len; k++)
{
if(str[i].find(str[j][k])!=string::npos)
{
cnt++;
}
}
mp[i][j]=cnt;
}
} }
void dfs(int depth,int sum)//depth表示深度,sum表示当前搜索过程中的最小值
{
if(sum>output||depth>=m)//剪枝
return ;
for(int i=,len=str.size(); i<len; i++)
{
if(!vis[i])
{
ans[depth]=i;
vis[i]=true;
if(depth>&&depth<m)
sum+=mp[ans[depth]][ans[depth-]];
if(depth<m-)
dfs(depth+,sum);
else{
output=min(output,sum);//比较最小值
sum=;
}
if(depth>&&depth<m)
sum-=mp[ans[depth]][ans[depth-]];
vis[i]=false;//标记还原
}
}
}
void init()//初始化
{
str.clear();
memset(mp,,sizeof(mp));
memset(vis,,sizeof(vis));
}
int main()
{
while(cin>>m)
{
init();
string arr;
output=MAXN;
for(int i=; i<m; i++)
{
cin>>arr;
str.push_back(arr);
}
ask_Qpoint();
dfs(,);
cout<<output<<endl; }
}
ACM Dance Recital(dfs+剪枝)的更多相关文章
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
随机推荐
- Mybatis整合Spring -- typeAliasesPackage
Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...
- mmap实现大文件快速拷贝
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- docker把web jar包制作成镜像
1.新建一个spring boot项目 并使用maven打成jar包,放到linux(centos7)环境上. 运行 java -jar hello.jar 后结果如下(这里项目对外提供的端口是90 ...
- vue.js 源代码学习笔记 ----- html-parse.js
/** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...
- Linux 进程、线程运行在指定CPU核上
/******************************************************************************** * Linux 进程.线程运行在指定 ...
- 为什么要使用索引?-Innodb与Myisam引擎的区别与应用场景
Innodb与Myisam引擎的区别与应用场景 http://www.cnblogs.com/changna1314/p/6878900.html https://www.cnblogs.com/ho ...
- java中读取配置文件
若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...
- Java基础之一:Java开发环境配置
Java 开发环境配置 window系统安装java 下载JDK,地址:http://www.oracle.com 在下载页面中你需要选择接受许可,并根据自己的系统选择对应的版本,本文以 Window ...
- angular指令的详细讲解,不断补充
独立作用域:就是在指令中设置了scope: **** ·false 共享父作用域 ·true 继承父作用域,并且新建独立作用域 ·object 不继承父作用域,创建新的独立作用域 一般来说我们会使用第 ...
- [C++ Primer] 第8章: IO库
IO类 iostream定义了读写流的基本类型 istream, wistream 从流读取数据 ostream, wostream 向流写入数据 iostream, wiostream 读写流 fs ...