【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game
题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035
思路:
一圈人围坐,给出小球的传递序列,求解最少有多少个人。
问题简单化:如果有一排人,单向传递,给出序列求解最少多少人。那么问题就是:求解一个最短序列使得给出的所有序列都是该序列的连续子序列。
再分别解决以下问题:
- 第一个人可以向左向右传递: 每个传递序列有两种情况,分别是正序与逆序。因为不清楚当前序列是向左传还是向右传。
- 传递是一个环:规定将第一个序列正向串作为首序列,累计答案的时候只需要算上重叠部分即可。
- 输入可能是绕过好几圈的情况:如果绕过好几圈,那么其他的字串因已经被包含而舍弃,dp过程不会进行,最后累计ans时ans=len[0]-重叠(s[0][0],s[0][0])即最小循环节的长度。
- 输入保证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的更多相关文章
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection
UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 12170 Easy Climb
UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路: 引别人一 ...
- 【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
- ACM - 动态规划 - UVA 1347 Tour
UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...
随机推荐
- CSS样式表引用方式
最近讲课中,有些学员对调用样式表老是有含糊不清?大体说来有四种方式: 1.外部文件引用方式;(推荐使用) 2.使用@import引用外部CSS文件; 3.内部文档头方式也叫内嵌法调用; 4.直接插入式 ...
- html css 如何将表头固定(转)
html css 如何将表头固定 position属性取值为fixed时,则元素的位置将不受滚动条的影响,而是直接依据窗口定位,这就是将表头固定的最直接方法,网上其他途径感觉都是在走弯路.但是与此同时 ...
- JavaScript 踩坑心得— 为了高速(上)
一.前言 很多情况下,产品的设计与开发人员一直想打造一套高品质的解决方案,从而快速.平稳地适应产品迭代.速度是衡量产品适应性的真正且唯一的标准,而且,这并不是笔者的一家之言. 「速度是衡量适应能力的真 ...
- HDU4756+Prim
题意简单:去掉最小生成树的某一条边并补上一条,求MaxVal 思路:贪心(借鉴Yamidie的思路...) 分别求出最小生成树和次最小生成树,再在这两棵树上求最小生成树 #include<std ...
- 【安全组网】思科IOS设备基础应用
思科IOS有2种主要命令行模式:用户模式与特权模式 1.用户模式(user mode),当用“>”表示实在用户模式下 2.特权模式(exec mode),当用"#"表示是在特 ...
- Task-based Asynchronous Pattern (TAP)
The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Thr ...
- pylinter could not automatically determined the path to `lint.py`
先关闭Sublime Text 1) 到官网先下载pylinter,http://www.logilab.org/project/pylint,然后解压缩,拷贝到C盘,目录为C:\pylint-1.0 ...
- extends:类似于java中的继承特征,extends="struts-default"
extends:类似于java中的继承特征,extends="struts-default"就是继承struts-default.xml,它里面定义了许多跳转类型.拦截器等一些常用 ...
- 函数page_align
/************************************************************//** Gets the start of a page. @return ...
- poj3255,poj2449
这里介绍怎么求k短路 A*搜索 估价函数f[i]=g[i]+h[i]; 在这里g[i]表示到达点i当前路径长,h[i]表示点i到达终点的最短距离 在搜索中,每次都取队列估价函数值最小的点,然后把它所能 ...