zoukankan      html  css  js  c++  java
  • 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(最小值,就不用继续搜了(剪枝);

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<vector>
     5 #include<algorithm>
     6 #include<map>
     7 using namespace std;
     8 typedef pair<string,string>pair1;
     9 const int MAXN=1e3+10;
    10 int m,n,sum,output=MAXN;
    11 int vis[MAXN];//标记数组
    12 vector<string>str;
    13 int ans[MAXN];//记录搜索的顺序
    14 int mp[MAXN][MAXN];//第i个字符串和第j个字符串之间的匹配度
    15 void ask_Qpoint()//求任意两个字符串之间的匹配度
    16 {
    17     for(int i=0; i<m; i++)
    18     {
    19         for(int j=0; j<m; j++)
    20         {
    21             int cnt=0;
    22             for(int k=0,len=str[j].size(); k<len; k++)
    23             {
    24                 if(str[i].find(str[j][k])!=string::npos)
    25                 {
    26                     cnt++;
    27                 }
    28             }
    29          mp[i][j]=cnt;
    30         }
    31     }
    32 
    33 }
    34 void dfs(int depth,int sum)//depth表示深度,sum表示当前搜索过程中的最小值
    35 {
    36 if(sum>output||depth>=m)//剪枝
    37     return ;
    38     for(int i=0,len=str.size(); i<len; i++)
    39     {
    40         if(!vis[i])
    41         {
    42             ans[depth]=i;
    43             vis[i]=true;
    44             if(depth>0&&depth<m)
    45                 sum+=mp[ans[depth]][ans[depth-1]];
    46             if(depth<m-1)
    47                 dfs(depth+1,sum);
    48              else{
    49             output=min(output,sum);//比较最小值
    50             sum=0;
    51              }
    52             if(depth>0&&depth<m)
    53                 sum-=mp[ans[depth]][ans[depth-1]];
    54             vis[i]=false;//标记还原
    55         }
    56     }
    57 }
    58 void init()//初始化
    59 {
    60     str.clear();
    61     memset(mp,0,sizeof(mp));
    62     memset(vis,0,sizeof(vis));
    63 }
    64 int main()
    65 {
    66     while(cin>>m)
    67     {
    68         init();
    69         string arr;
    70         output=MAXN;
    71         for(int i=0; i<m; i++)
    72         {
    73             cin>>arr;
    74             str.push_back(arr);
    75         }
    76         ask_Qpoint();
    77         dfs(0,0);
    78         cout<<output<<endl;
    79 
    80     }
    81 }
  • 相关阅读:
    多台计算机之间数据同步——1.[转]网线制作图解教程
    离心泵的使用注意事项泄露或未排气造成扬程不够
    家庭上网用路由器和ADSL的连接
    专业FLV地址解析
    [求助]带程序访问控制的防火墙 eTrust Personal Firewall 和卡巴斯基2009引起冲突造成系统频繁死机
    DV录像带导出一定要用1394
    Cursor:url()的使用
    理解并解决JavaScript内存泄漏
    CodeIgniter的HMVC
    关于在IE下JavaScript的 Stack overflow at line 错误可能的原因
  • 原文地址:https://www.cnblogs.com/moomcake/p/9921183.html
Copyright © 2011-2022 走看看