Codeforces Round #338 (Div. 2) C. Running Track dp
C. Running Track
题目连接:
http://www.codeforces.com/contest/615/problem/C
Description
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.
Input
First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.
Output
The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following n lines should contain two integers xi and yi — numbers of ending blocks in the corresponding piece. If xi ≤ yi then this piece is used in the regular order, and if xi > yi piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.
Sample Input
abc
cbaabc
Sample Output
2
3 1
1 3
Hint
题意
给你一个字符串s,然后给你字符串t,让你用s中的子串来构成t。
并输出构造方案。
s可以旋转
题解:
我们可以直接跑dp,预处理dp1[i][j],表示匹配到s串的i位置,t串的j位置时候,最多可以往前跳dp1[i][j]个字符。
其实这个就是最长公共前缀的dp。
跑完之后,我们就可以跑第二个dp了,dp[i]表示以t的i位置结尾所需要的最少次数。
很显然dp[i] = max(dp[i],dp[i-dp1[j][i]]+1);
然后记录一下过程,倒着跑一下DP,把路径输出就好了。
代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 3000
long long dp1[maxn][maxn];
long long dp2[maxn][maxn];
char a[maxn],b[maxn],c[maxn];
long long dp[maxn];
int step[maxn];
int ans[maxn][2];
int main()
{
scanf("%s%s",a+1,b+1);
int len1 = strlen(a+1);
int len2 = strlen(b+1);
for(int i=1;i<=len2;i++)
dp[i]=1e9;
for(int i=1;i<=len1;i++)
c[i]=a[len1-i+1];
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(a[i]==b[j])dp1[i][j]=dp1[i-1][j-1]+1;
if(b[j]==c[i])dp2[i][j]=dp2[i-1][j-1]+1;
}
}
for(int i=1;i<=len2;i++)
{
for(int j=1;j<=len1;j++)
{
if(dp1[j][i])
{
if(dp[i]>dp[i-dp1[j][i]]+1)
{
dp[i]=dp[i-dp1[j][i]]+1;
step[i]=i-dp1[j][i];
}
}
if(dp2[j][i])
{
if(dp[i]>dp[i-dp2[j][i]]+1)
{
dp[i]=dp[i-dp2[j][i]]+1;
step[i]=i-dp2[j][i];
}
}
}
}
if(dp[len2]==1e9)return puts("-1");
printf("%d\n",dp[len2]);
int sum = dp[len2];
int now = len2;
while(sum--)
{
for(int i=1;i<=len1;i++)
{
if(dp1[i][now]==now-step[now])
{
ans[sum][0]=i-dp1[i][now]+1,ans[sum][1]=i;
now = step[now];
break;
}
if(dp2[i][now]==now-step[now])
{
ans[sum][0]=len1-i+dp2[i][now],ans[sum][1]=len1-i+1;
now = step[now];
break;
}
}
}
for(int i=0;i<dp[len2];i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
}
Codeforces Round #338 (Div. 2) C. Running Track dp的更多相关文章
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...
- Codeforces Round #338 (Div. 2)
水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #338 (Div. 2) E. Hexagons 讨论讨论
E. Hexagons 题目连接: http://codeforces.com/contest/615/problem/E Description Ayrat is looking for the p ...
- Codeforces Round #338 (Div. 2) D. Multipliers 数论
D. Multipliers 题目连接: http://codeforces.com/contest/615/problem/D Description Ayrat has number n, rep ...
- Codeforces Round #338 (Div. 2) A. Bulbs 水题
A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...
- Codeforces Round #338 (Div. 2) D 数学
D. Multipliers time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #338 (Div. 2) B dp
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP
B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...
随机推荐
- <转>如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等
原文链接:http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB) ...
- VC6兼容性及打开文件崩溃问题解决
VC6虽然老,但是一些工程还非得用它打开,没办法…… 今天偶然用到,因为新装了系统,之前的问题又要重新解决一遍 在这记录下解决过程,方便以后查阅: 一.兼容问题: XP以上windows系统打开VC6 ...
- 一个Web页面的生命周期 ,面试常常被问到
常规页生命周期阶段 一般来说,页要经历下表概述的各个阶段.除了页生命周期阶段以外,在请求前后还存在应用程序阶段,但是这些阶段并不特定于页.有关更多信息,请参见 ASP.NET 应用程序生命周期概述. ...
- CSS抗锯齿 font-smoothing
CSS3里面加入了一个“-webkit-font-smoothing”属性. 这个属性可以使页面上的字体抗锯齿,使用后字体看起来会更清晰舒服. 加上之后就顿时感觉页面小清晰了. 淘宝也在用哦! 它有三 ...
- 使用nodejs中httpProxy代理时候出现404异常
在公司中使用nodejs构建代理服务器实现前后台分离,代码不能拿出来,然后出现httpProxy代理资源的时候老是出现404.明明被代理的接口是存在的.代码大概如下: var http = requi ...
- C# 必应代码搜索
微软宣布推出必应代码搜索服务,暂时只支持 C# 语言,日后将支持更多代码语言. Visual Studio 用户安装必应搜索插件之后可使用该服务来简化编程任务.Visual Studio 与 MSDN ...
- jquery属性选择器中|value和^value的区别
jquery的属性选择中有两个比较混淆:一个是[attribute^value], 另一个是[attribute|value]. 先看解释: [attribute^value]:选取属性的值以valu ...
- C++Bulder DataSnap 内存泄露元凶
DSServerClass1 DSServerClass1DestroyInstance void __fastcall TServerContainer1::DSServerClass1Destro ...
- Netbeans Platform 工程,免安装JDK
使用Netbeans 6.8 创建了一个Netbeans Platform 工程,以Zip形式发布后, 按照以下操作,可 以在客户端免安装JDK: 1. 从已安装JDK的计算机中,提取JDK:eg. ...
- redis缓存数据表
直观上看,数据库中的数据都是按表存储的:更微观地看,这些表都是按行存储的.每执行一 次select查询,数据库都会返回一个结果集,这个结果集由若干行组成.所以,一个自然而然 的想法就是在Redis中找 ...