Codeforces Round #338 (Div. 2) 615C Running Track(dp)

C. Running Track
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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 test(s)
input
abc
cbaabc
output
2
3 1
1 3
input
aaabrytaaa
ayrat
output
3
1 1
6 5
8 7
input
ami
no
output
-1
Note

In the first sample string "cbaabc" = "cba" + "abc".

In the second sample: "ayrat" = "a" + "yr" + "at".



题目链接:http://codeforces.com/contest/615/problem/C

给出两个字符串, 输出尽量少的n个区间, 使得第一个字符串在此区间匹配第二个字符串.

dp1记录s2倒序的最长匹配, dp2记录s2正序的最长匹配. 而后遍历s2, 每次求得最长区间, 可以使得 n 尽量的小.

AC 代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 2222;
string s1, s2;
int dp1[MAXN][MAXN], dp2[MAXN][MAXN], cnt;
std::vector<pair<int, int> > ans;
int main(int argc, char const *argv[])
{
    cin >> s1 >> s2;
    int len1 = s1.size(), len2 = s2.size();
    for(int i = len2 - 1; ~i; --i) 
        for(int j = len1 - 1; ~j; --j)
            if(s1[j] == s2[i]) dp1[i][j] = max(dp1[i][j], dp1[i + 1][j + 1] + 1);
    for(int i = len2 - 1; ~i; --i)
        for(int j = 0; j < len1; ++j)
            if(s1[j] == s2[i]) dp2[i][j] = max(dp2[i][j], (j ? dp2[i + 1][j - 1] : 0) + 1);
    while(cnt < len2) {
        int x = 0, l = 0, r = 0;
        for(int i = 0; i < len1; ++i)
            if(x < dp1[cnt][i]) {
                x = dp1[cnt][i];
                l = i + 1;
                r = i + x;
            }
        for(int i = 0; i < len1; ++i)
            if(x < dp2[cnt][i]) {
                x = dp2[cnt][i];
                l = i + 1;
                r = i + 2 - x;
            }
        if(!x) {
            cout << -1 << endl;
            return 0;
        }
        ans.push_back(make_pair(l, r));
        cnt += x;
    }
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); ++i)
        cout << ans[i].first << ' ' << ans[i].second << endl;
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值