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
2 3 1 1 3
aaabrytaaa ayrat
3 1 1 6 5 8 7
ami no
-1
Hint
In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at".
題目意思可以直接從數據看出來。
這道題其實只需要暴力匹配+貪心就好了,每次匹配最長的子串,因爲是連續子串,
如果長度長的子串能匹配到,那麼短的肯定也能匹配到,所以我這裏使用了kmp匹配。
#include <stdio.h>
#include <string.h>
#define DBUG printf ( "Here!\n" )
const int maxn = 2105;
char str[maxn], ch[maxn], temp[maxn];
int next[maxn], ans[maxn][2], l, r;
void reverse ( char * str )
{
for ( int i = 0, j = strlen ( str )-1; i < j; i ++, j -- )
{
char ch = str[i];
str[i] = str[j];
str[j] = ch;
}
}
void get_next ( char * ch )
{
int i, j, len = strlen ( ch );
next[0] = i = -1, j = 0;
while ( j < len )
{
if ( i == -1 || ch[i] == ch[j] )
{
i ++;
j ++;
if ( ch[i] == ch[j] )
next[j] = next[i];
else
next[j] = i;
}
else
i = next[i];
}
}
template < class T >
void swap ( T & a, T & b )
{
T t = a;
a = b;
b = t;
}
void print ( int n )
{
for ( int i = 0; i < n; i ++ )
printf ( "%d ", next[i] );
printf ( "\n" );
}
int kmp ( char * str, char * ch ) //kmp
{
int ls = strlen ( str ), lc = strlen ( ch );
if ( ls < lc )
return 0;
get_next ( ch );
int i, j, tl, tr;
tl = tr = i = j = 0;
//print ( lc );
while ( i < ls && j < lc )
{
if (j == -1 || str[i] == ch[j] )
{
tr = i;
i ++;
j ++;
}
else
{
j = next[j];
if ( j == -1 )
tl = i+1;
else
{
tl = tr-j+1; //在右邊左移了j-1位
}
}
}
if ( j >= lc )
{
//printf ( "%s %d %d\n", ch, tl, tr );
l = tl;
r = tr;
return 1;
}
return 0;
}
int main ( )
{
scanf ( "%s%s", str, ch );
int pos = 0, ls = strlen ( str ), lc = strlen ( ch );
int cnt = 0;
while ( pos < lc )
{
int i, tag = 0, res;
for ( i = 1; pos+i <= lc; i ++ ) //無法匹配的時候退出
{
int ok = 0;
for ( int j = pos; j < pos+i; j ++ )
temp[j-pos] = ch[j];
temp[i] = '\0';
if ( kmp ( str, temp ) ) tag = ok = 1, res = 0;
if ( ok )
continue ;
reverse ( temp ); //反轉
//puts ( temp );
if ( kmp ( str, temp ) ) res = tag = ok = 1; //res標記
if ( ! ok )
break ;
}
if ( tag )
{
//printf ( "%d %d\n", l, r );
if ( res )
swap ( l, r );
ans[cnt][0] = l+1;
ans[cnt ++][1] = r+1;
pos = pos+i-1;
}
else //一個字符都無法匹配
break ;
}
if ( pos < lc )
{
printf ( "-1" );
return 0;
}
printf ( "%d\n", cnt );
for ( int i = 0; i < cnt; i ++ )
printf ( "%d %d\n", ans[i][0], ans[i][1] );
return 0;
}
/*
特殊數據:
ababababababababba
abbabbabababba
*/