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的更多相关文章

  1. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp

    B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...

  2. Codeforces Round #338 (Div. 2)

    水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...

  3. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  4. Codeforces Round #338 (Div. 2) E. Hexagons 讨论讨论

    E. Hexagons 题目连接: http://codeforces.com/contest/615/problem/E Description Ayrat is looking for the p ...

  5. Codeforces Round #338 (Div. 2) D. Multipliers 数论

    D. Multipliers 题目连接: http://codeforces.com/contest/615/problem/D Description Ayrat has number n, rep ...

  6. Codeforces Round #338 (Div. 2) A. Bulbs 水题

    A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...

  7. Codeforces Round #338 (Div. 2) D 数学

    D. Multipliers time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. Codeforces Round #338 (Div. 2) B dp

    B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  9. 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 ...

随机推荐

  1. 字节顺序重置及“#include <algorith.h>”相关的STL最重要的头文件提醒

    这两天在写一个程序,需要将二进制文件中的数据以指定结构读入内存,说明文档中有提到大端序和小端序(Big Endian or Little Endian) 的概念,就找了一下字节顺序重置的算法,在一篇名 ...

  2. Treap树

    Treap树算是一种简单的优化策略,这名字大家也能猜到,树和堆的合体,其实原理比较简单,在树中维护一个"优先级“,”优先级“ 采用随机数的方法,但是”优先级“必须满足根堆的性质,当然是“大根 ...

  3. jQuery Mobile 页面事件总结

    一.页面初始化事件(Page initiallization) 在页面创建前,当页面创建时,以及在页面初始化之后.只在第一次加载时执行. 1. pagebeforecreate 页面创建前 [sour ...

  4. FTP没权限直接删除目录,写的一个小工具循环删除

    $path = '/var/www/html/Runtime/'; $ite = new RecursiveDirectoryIterator($path); foreach (new Recursi ...

  5. 在fedora20下配置hadoop2.5.1的eclipse插件

    (博客园-番茄酱原创) 在我的系统中,hadoop-2.5.1的安装路径是/opt/lib64/hadoop-2.5.1下面,然后hadoop-2.2.0的路径是/home/hadoop/下载/had ...

  6. 第二百二十七天 how can I 坚持

    今天去了蟒山,天池,刚去的时候身体有点难受,整天都是那样,回来就好多了,不知道怎么回事. 天池竟然是个人造池,挺大,没有去十三陵,回来都很晚了. 去天池竟然是走的小路,已经关了,不让进,里边玲玲清清的 ...

  7. mysql innobackupex xtrabackup 大数据量 备份 还原(转)

    原文:http://blog.51yip.com/mysql/1650.html 作者:海底苍鹰 大数据量备份与还原,始终是个难点.当MYSQL超10G,用mysqldump来导出就比较慢了.在这里推 ...

  8. 【JSON】JSON字符串的操作(不断积累中)

    一.JS遍历JSON串 示例01 目标:返回的JSON串中,一个Key对应的Value是一个数组(若在Java程序中,是一个List). 现在要求获取每个数组中的第一个对象元素中的name属性的值. ...

  9. 微软IOC容器Unity简单代码示例3-基于约定的自动注册机制

    @(编程) [TOC] Unity在3.0之后,支持基于约定的自动注册机制Registration By Convention,本文简单介绍如何配置. 1. 通过Nuget下载Unity 版本号如下: ...

  10. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...