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 ...
随机推荐
- java 获取本机的IP地址
方法一:这种方式有一定的局限性,在Linux下的执行结果是:本机的IP = xxx/127.0.1.1 (其中xxx是你的计算机名) public void getLocalIPAddress() { ...
- 3321 Apple Tree 树状数组
LIANJIE:http://poj.org/problem?id=3321 给你一个多叉树,每个叉和叶子节点有一颗苹果.然后给你两个操作,一个是给你C清除某节点上的苹果或者添加(此节点上有苹果则清除 ...
- [例1.10]使用setw设置输出宽度的例子
[例1.10]使用setw设置输出宽度的例子: #include <iostream> #include <iomanip> using namespace std; void ...
- maven+jmeter+jenkins集成
马上要国庆了,最近比较忙,但是感觉忙的效率很一般,之前写过ant的集成,这两天研究了下maven,其中核心的插件便是jmeter-maven-plugin,要想了解更多的朋友,可以自行去官网wiki学 ...
- 新转移注意(caffe):ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory
https://github.com/NVIDIA/DIGITS/issues/8 For this error ImportError: libcudart.so.7.0: cannot open ...
- Linux性能评估命令
Linux性能评估工具 https://www.cnblogs.com/dianel/p/10085454.html Linux性能评估工具 目录 介绍 负载:uptime 查看内核的信息: dmes ...
- Idea问题记录
1.warning提示idea found duplicate code 打开 Settings → Editor → Inspections. 在出现的搜索栏处搜索 Duplicated Code ...
- 【idea】如何破解idea
1.IntelliJ IDEA官网下载 https://www.jetbrains.com/idea/download/ 2.安装IntelliJ IDEA 3.永久破解 在http://idea.l ...
- v4l2框架函数调用关系
所有的设备节点和子设备节点都是通过__video_register_device()注册的 1.对于video设备节点: 用户空间ioctl(VIDIOC_S_FMT)---> v4l2_fop ...
- bootstrap class sr-only 什么意思?
bootstrap class sr-only 什么意思? 在看 bootstrap 内联表单时,label 有一个 class 是 sr-only. sr-only 是给屏幕阅读器用的,是给视力不方 ...