UVa 10618 Fun Game

题目:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035

思路:

  一圈人围坐,给出小球的传递序列,求解最少有多少个人。

问题简单化:如果有一排人,单向传递,给出序列求解最少多少人。那么问题就是:求解一个最短序列使得给出的所有序列都是该序列的连续子序列。

再分别解决以下问题:

  1. 第一个人可以向左向右传递: 每个传递序列有两种情况,分别是正序与逆序。因为不清楚当前序列是向左传还是向右传。
  2. 传递是一个环:规定将第一个序列正向串作为首序列,累计答案的时候只需要算上重叠部分即可。
  3. 输入可能是绕过好几圈的情况:如果绕过好几圈,那么其他的字串因已经被包含而舍弃,dp过程不会进行,最后累计ans时ans=len[0]-重叠(s[0][0],s[0][0])即最小循环节的长度。
  4. 输入保证n>=2:当ans<=1是修改为2

代码:

 

 #include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = ;
const int maxlen= + ; struct Node{
string s,rev;
bool operator <(const Node& rhs) const{
return s.size()<rhs.s.size(); //size由小到大
}
}; int n;
int d[<<maxn][maxn][];
int overlap[maxn][maxn][][];
string s[maxn][];
int len[maxn]; //return a右 && b左的重叠部分
int make_overlap(const string& a,const string& b){
int n1=a.size();
int n2=b.size();
FOR(i,,n1) { //找到最小重合点 即返回最大重合长度
if(n2 + i <= n1) continue;
bool ok=true;
for(int j=;i+j<n1;j++)
if(a[i+j] != b[j]) { ok=false; break; }
if(ok) return n1-i;
}
return ; //无重合
} void init() {
Node nodes[maxn];
FOR(i,,n) {
cin>>nodes[i].s;
nodes[i].rev=nodes[i].s;
reverse(nodes[i].rev.begin(),nodes[i].rev.end()); //反转串
} sort(nodes,nodes+n); //sort
int nn=;
FOR(i,,n) {
bool need=true;
FOR(j,i+,n)
if(nodes[j].s.find(nodes[i].s) != string::npos ||
nodes[j].rev.find(nodes[i].s) != string::npos){ //如果被包含则舍弃
need=false; break;
}
if(need) {
s[nn][]=nodes[i].s;
s[nn][]=nodes[i].rev;
len[nn]=s[nn][].size();
nn++;
}
}
n=nn; //构造重叠数组
FOR(i,,n) FOR(x,,)
FOR(j,,n) FOR(y,,)
overlap[i][j][x][y]=make_overlap(s[i][x],s[j][y]);
} inline void update(int& x,int v) {
if(x< || v<x) x=v;
} void solve() {
memset(d,-,sizeof(d));
d[][][]=len[]; //始终把s[0][0]放在首位 int full=<<n;
FOR(s,,full)
FOR(i,,n) FOR(x,,) if(d[s][i][x]>=) //已求过集合中的枚举
FOR(j,,n) if(!((<<j) & s)) FOR(y,,) //刷表更新的目标枚举
update(d[s|(<<j)][j][y], d[s][i][x] + len[j]-overlap[i][j][x][y]); int ans=-; full--;
FOR(i,,n) FOR(x,,) {
if(d[full][i][x]<) continue;
update(ans, d[full][i][x] - overlap[i][][x][]); //ans累计 枚举尾字串并减去与第一个串的重叠部分
}
if(ans<=) cout<<"2\n"; //题目中明确给出 n>=2
else
cout<<ans<<"\n";
}
int main(){
while(cin>>n && n) {
init();
solve();
}
return ;
}

【暑假】[深入动态规划]UVa 10618 Fun Game的更多相关文章

  1. 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

    UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...

  2. 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection

    UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...

  3. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  4. 【暑假】[深入动态规划]UVa 1628 Pizza Delivery

    UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:    ...

  5. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  6. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  7. 【暑假】[深入动态规划]UVa 1627 Team them up!

    UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma ...

  8. 【暑假】[深入动态规划]UVa 1412 Fund Management

    UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   ...

  9. ACM - 动态规划 - UVA 1347 Tour

    UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...

随机推荐

  1. yum安装gcc

    如果服务器是自己的,并且机器就在身边,那什么都不用说了,缺少gcc顶多就是重新放入安装盘,把开发工具包安装上.但是如果是租的服务器,托管服务 方那帮人又搞不懂你说的啥子gcc,要安装gcc实在是太麻烦 ...

  2. <base target="_blank"/>

    <base target=_blank> 是将基本链接的目标框架都改为新页打开

  3. 团体程序设计天梯赛-练习集L1-022. 奇偶分家

    L1-022. 奇偶分家 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定N个正整数,请统计奇数和偶数各有多少个? 输入格式 ...

  4. spoj 379

    题是水题  但丫的题目意思太难懂 .......   英语水平  ...... #include <cstdio> #include <cstring> #include &l ...

  5. 李洪强漫谈iOS开发[C语言-014]-变量

    01 变量的概念 02 - 变量的语法 03 变量的使用

  6. xcode 树形管理 cocos2d-x的资源

    把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any  added folders", 而不是默认的&q ...

  7. Winform 数据验证

    http://blog.scosby.com/post/2010/02/11/Validation-in-Windows-Forms.aspx 总结:1. CancelEventArgs e ,调用e ...

  8. MapReduce编程系列 — 1:计算单词

    1.代码: package com.mrdemo; import java.io.IOException; import java.util.StringTokenizer; import org.a ...

  9. 拔高你的Java代码质量吧:推荐使用枚举定义常量(转)

    提高你的Java代码质量吧:推荐使用枚举定义常量 一.分析 常量的声明是每一个项目中不可或缺的,在Java1.5之前,我们只有两种方式的声明:类常量和接口常量.不过,在1.5版之后有了改进,即新增了一 ...

  10. FastScroll(1)ListView打开FastScroll及自定义它的样式

    打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...