Codeforces 615C Running Track 【模拟匹配】

来源:互联网 发布:国内航空英语翻译软件 编辑:程序博客网 时间:2024/06/06 01:23

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 she 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
abccbaabc
output
23 11 3
input
aaabrytaaaayrat
output
31 16 58 7
input
amino
output
-1
Note

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

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



题意:给你两个字符串s和t,每次你可以从完整的s串中任意截取一段连续的串接上去(接之前可以翻转一下),问你最少需要截取多少次可以构成串t,输出每次截取的起点x和终点y,若x < y说明截取后直接接后面,x > y说明接之前先翻转。


还以为3层时间复杂度会T,一直YY2层的算法。


思路:暴力模拟匹配,正着反着各扫一遍匹配。


AC代码:


#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (2100+10)#define MAXM (200000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 10007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)using namespace std;char s[MAXN], ss[MAXN], t[MAXN];int ans[MAXN][2];int main(){    Rs(s); Rs(t);    strcpy(ss, s); strrev(ss);    int ls = strlen(s); int lt = strlen(t);    int num = 0;    for(int i = 0; i < lt;)    {        int nextpos = -1; int p1, p2;        for(int j = 0; j < ls; j++)        {            int k = j; int next = i;            while(next < lt && s[k] == t[next]) next++, k++;            //next--;            if(next > nextpos) {nextpos = next, p1 = j+1, p2 = k;}        }        for(int j = 0; j < ls; j++)        {            int k = j; int next = i;            while(next < lt && ss[k] == t[next]) next++, k++;            //next--;            if(next > nextpos) {nextpos = next, p1 = ls-j, p2 = ls-k+1;}        }        if(i == nextpos) {Pi(-1); return 0;}        i = nextpos; ans[num][0] = p1, ans[num++][1] = p2;    }    Pi(num);    for(int i = 0; i < num; i++)        printf("%d %d\n", ans[i][0], ans[i][1]);    return 0;}


0 0
原创粉丝点击