Codeforces 615 C Running Track【KMP匹配】

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.

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


题目大意:

将b串分解成若干的子串(子串不能重叠,不能跨越,必须连续的),使得子串可以正序出现在a串中,或者是逆序出现在a串中,问最少能够分解出来多少个子串,并且输出这些个子串在a串中的位子、如果不能拆成这样的结果,输出-1。

分析样例2:

ayrat==a+yr+at;其中a在a串中出现的位子是1 1,yr在a串中出现的位子是6 5(逆序),at在a串中的位子是8 7(逆序);


思路;


1、首先我们确定两个可匹配串:a以及a的反串。那么如果我们现在假设拿出了一个子串:b,b在a中匹配的终点是位子i,那么其起点就是i-lenb+1;记录答案:ans【】【0】=i-lenb+1,ans【】【1】=i;那么如果我们b在a的反串中的匹配的终点的位子是i,那么其起点和终点分别记录为:ans【】【0】=lena-(i-lenb+1)+1,ans【】【1】=lenaa-i+1;


2、那么我们暴力枚举b串(子串),设定左区间为ll,右区间为rr,首先设定ll=0,rr=0;如果当前串b(第一个字符)在a中或者a的反串中有所匹配,那么我们使rr++,然后继续匹配,如果当前b串在a中或者a的反串中都没得匹配,那么我们令ll=rr,并且记录最终第一个分出来的子串的答案。然后一直进行下去。当进行到ll==rr并且串b在a或者a的反串都没得匹配的时候,显然就是-1的情况,特殊判定一下即可。


Ac代码(略挫):

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int nextt[40000];
char raa[40000];
char aa[40000];
char bb[40000];
char cc[40000];
char a[40000];
char b[4000];
int ans[400000][2];
int lena,lenb,contz,flagr,lenaa,lenbb;
void set_naxt()//子串的nextt数组
{
    int i=0,j=-1;
    nextt[0]=-1;
    while(i<lenb)
    {
        if(j==-1||b[i]==b[j])
        {
            i++; j++;
            nextt[i]=j;
        }
        else
        j=nextt[j];
    }
}

int KMP()
{
    int i=0,j=0;
    set_naxt();
    while(i<lena)
    {
        if(j==-1||a[i]==b[j])
        {
            i++;j++;
        }
        else
        j=nextt[j];
        if(j==lenb)
        {
            if(flagr==0)
            {
                ans[contz][0]=i-lenb+1;
                ans[contz][1]=i;
            }
            else
            {
                ans[contz][0]=lenaa-(i-lenb+1)+1;
                ans[contz][1]=lenaa-i+1;
            }
            return 1;
        }
    }
    return -1;
}
//b匹配a
int main()
{
    while(~scanf("%s%s",raa,bb))
    {
        contz=0;
        lenaa=strlen(raa);
        lenbb=strlen(bb);
        strcpy(aa,raa);
        reverse(raa,raa+lenaa);
        int ll=0;
        int rr=0;
        while(ll<lenbb)
        {
            int flag=0;
            while(rr<lenbb)
            {
                strcpy(a,aa);
                flagr=0;
                int cont=0;
                for(int j=ll;j<=rr;j++)
                {
                    b[cont++]=bb[j];
                }
                b[cont]='\0';
                lena=lenaa;
                lenb=rr-ll+1;
                if(KMP()==1)
                {
                    flag=1;
                    rr++;
                    continue;
                }
                else
                {
                    strcpy(a,raa);
                    flagr=1;
                    int cont=0;
                    lena=lenaa;
                    lenb=rr-ll+1;
                    for(int j=ll;j<=rr;j++)
                    {
                        b[cont++]=bb[j];
                    }
                    b[cont]='\0';
                    if(KMP()==1)
                    {
                        flag=1;
                        rr++;
                        continue;
                    }
                    else  break;
                }
                if(flag==0)break;
            }
            if(flag==0)break;
            contz++;
            ll=rr;
        }
        if(ll==lenbb)
        {
            printf("%d\n",contz);
            for(int i=0;i<contz;i++)
            {
                printf("%d %d\n",ans[i][0],ans[i][1]);
            }
        }
        else printf("-1\n");
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值