time limit per test1 second

memory limit per test512 megabytes

inputstandard input

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

Examples

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

【题解】



把s1串的所有子串都加入到字典树;在每个节点记录这个节点在s1中的起始位置和终止位置;

加入的时候正串和反串都要加入(加入到同一颗字典树即可);

因为s1串可以多次使用;

所以我们让每次使用都达到最长的距离即可,这样肯定是最优的;

那个from和to域实在设计得巧妙;

从s2串的初始位置开始;从字典树的根节点开始走一条路径即可;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long using namespace std; const int MAXN = 4500000;
const int MAXSIZE = 3000;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int tree1[MAXN][27],from[MAXN],to[MAXN];
int ans[MAXSIZE][2];
int cnt1 = 0,cnt2 = 0;
string s1,s2; void add1(int pos,int len)
{
int now = 0;
for (int i = pos;i<=len-1;i++)
{
int key = s1[i]-'a'+1;
if (tree1[now][key])
{
now = tree1[now][key];
continue;
}
tree1[now][key]=++cnt1;
now = tree1[now][key];
from[now] = pos;to[now] = i;
}
} void add2(int pos)
{
int now = 0;
for (int i = pos;i>=0;i--)
{
int key = s1[i]-'a'+1;
if (tree1[now][key])
{
now = tree1[now][key];
continue;
}
tree1[now][key]=++cnt1;
now = tree1[now][key];
from[now] = pos;to[now] =i;
}
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >>s1;
cin >>s2;
int len1 = s1.size();
for (int i = 0;i <= len1-1;i++)
add1(i,len1);
for (int i = len1-1;i>=0;i--)
add2(i);
int now = 0;
int len2 = s2.size();
int dd = 0;
while (now<=len2-1)
{
int key = s2[now]-'a'+1;
//printf("%d\n",now);
int temp1 = 0,num1 =now;
while (num1<=len2-1 && tree1[temp1][key]!=0)
{
temp1 = tree1[temp1][key];
num1++;
key = s2[num1] - 'a'+1;
}
if (num1-1 < now)
{
puts("-1");
return 0;
}
else
{
dd++;
now = num1-1;
ans[dd][0] = from[temp1];
ans[dd][1] = to[temp1];
now++;
}
}
printf("%d\n",dd);
for (int i = 1;i <= dd;i++)
printf("%d %d\n",ans[i][0]+1,ans[i][1]+1);
return 0;
}

【28.57%】【codeforces 615C】 Running Track的更多相关文章

  1. 【28.57%】【codeforces 711E】ZS and The Birthday Paradox

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【57.97%】【codeforces Round #380A】Interview with Oleg

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【30.93%】【codeforces 558E】A Simple Task

    time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  7. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 【codeforces 760A】Petr and a calendar

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 750C】New Year and Rating(做法2)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 关于python的拷贝

    https://blog.csdn.net/koukehui0292/article/details/82993958 Python的 深度拷贝: import copy d=copy.deepcop ...

  2. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  3. PyCharm下载主题以及个性化设置(详细)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一.下载主题 1.在http://www.themesmap.com/theme.html上选择自己喜欢的主题点进去后进行下载. ...

  4. 组件绑定v-model,实现最大化复用

    看优秀的vue项目,对组件的封装做的都非常到位,比如一个按钮都可以实现复用,仔细研究会发现实现基础就是组件直接绑定v-model,来看看按钮: 比如有个点赞按钮,长这样: 当点赞之后变成这样: 相信很 ...

  5. 9.5 Binder系统_驱动情景分析_transaction_stack机制

    参考文章:http://www.cnblogs.com/samchen2009/p/3316001.html test_server服务进程可能有多个线程,而在发送数据的时候handle只表示了那个进 ...

  6. 关于浏览器不能执行JavaScrip问题的反思

    今天在一篇博客(http://blog.csdn.net/u011043843/article/details/27959563)的时候,写着用于演示的Javascript代码不能再浏览器执行,非常是 ...

  7. CImage将图片转为指定像素大小

    CFileDialog fDlg(true, "jpg", "",   OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,   &q ...

  8. LIVE555源代码研究之四:MediaServer (一)

    LIVE555源代码研究之四:MediaServer (一) 从本篇文章開始我们将从简单server程序作为突破点,深入研究LIVE555源代码. 从前面的文章我们知道.不论什么一个基于LIVE555 ...

  9. php重建二叉树(函数缺省参数相关的都写在后面,比如array_slice函数中的$length属性,故第一个参数是操作的数组)

    php重建二叉树(函数缺省参数相关的都写在后面,比如array_slice函数中的$length属性,故第一个参数是操作的数组) 一.总结 牛客网和洛谷一样,是真的好用 二.php重建二叉树 输入某二 ...

  10. 关于JavaScript对象概念的总结

    原文 https://www.jianshu.com/p/88213b499c4b 大纲 前言 1.对象的相关概念 2.对象的创建(简单创建) 3.对象的属性 3.1.数据属性 3.2.访问器属性 4 ...