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+剪枝)的更多相关文章

  1. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5887 题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不 ...

  5. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  6. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  7. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  8. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. 运行MySQL远程连接

    方法一:修改MySQL自带的“mysql”数据库中的“USER”表 USE mysql; UPDATE USER SET HOST='%' WHERE USER='root'; SELECT * FR ...

  2. 051——VUE中自定义指令:directive

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. new BigDecimal(0.01) 与 new BigDecimal(String.valueOf(0.01))的区别 (转)

    转自:http://blog.csdn.net/major1985/article/details/50210293 一般我们使用BigDecimal进行比较精密的计算,我这里计算金额.注意使用dou ...

  4. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 13

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  5. L187 DKK2

    Why can millions of hairs grow from our heads, and yet our palms手掌 and the soles of our feet are as ...

  6. react中findDOMNode

    在使用react过程中,大家有时会那么这里的findDomNode是做什么的呢? import { findDomNode } from 'react-dom'; 简单来说是用来得到实际Dom的,因为 ...

  7. Java 工程师求职遇害|多一分警惕,少一份悲剧

    当朋友圈里满是战狼票房屡创新高的刷屏文章时,一则有关 Java 开发工程师李文星面试遇害的报道,却令人唏嘘不已.年仅23岁.正值青春年少.怀揣着通过打拼奋斗实现养家糊口梦想的大学毕业生,在初入职场的第 ...

  8. 阿里历年经典Java面试题汇总

    Volatile的特征: A.禁止指令重排(有例外) B.可见性 Volatile的内存语义: 当写一个volatile变量时,JMM会把线程对应的本地内存中的共享变量值刷新到主内存. 当读一个vol ...

  9. 20155322 2016-2017-2 《Java程序设计》第8周学习总结

    20155322 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第八周学习的主要内容是课本的第十四第十五章,主要学习了以下知识点: 了解NIO 会使用Cha ...

  10. 《DSP using MATLAB》Problem 3.3

    按照题目的意思需要利用DTFT的性质,得到序列的DTFT结果(公式表示),本人数学功底太差,就不写了,直接用 书中的方法计算并画图. 代码: %% -------------------------- ...