[codeforces906E]Reverses

time limit per test : 2 seconds
memory limit per test : 256 megabytes

分数:3200

Hurricane came to Berland and to suburbs Stringsvill. You are going to it to check if it’s all right with you favorite string. Hurrinace broke it a bit by reversing some of its non-intersecting substrings. You have a photo of this string before hurricane and you want to restore it to original state using reversing minimum possible number of its substrings and find out which substrings you should reverse.

You are given a string s s s — original state of your string and string t t t — state of the string after hurricane. You should select k k k non-intersecting substrings of t t t in such a way that after reverse of these substrings string will be equal s and k k k is minimum possible.

Input

First line of input contains string s s s and second line contains string t t t. Both strings have same length and consist of lowercase English letters. 1   ≤   ∣ s ∣   =   ∣ t ∣   ≤   5 ⋅ 1 0 5 1 ≤ |s| = |t| ≤ 5·10^5 1s=t5105
Output

In first line print k k k — minimum number of substrings you should reverse. Next output k k k lines. Each line should contain two integers l i l_i li, r i r_i ri meaning that you should reverse substring from symbol number l i l_i li to symbol r i r_i ri (strings are 1-indexed). These substrings shouldn’t intersect. If there are multiple answers print any. If it’s impossible to restore string output − 1 -1 1.

Example
Input

abcxxxdef
cbaxxxfed

Output

2
7 9
1 3

题意:
给定两个字符串 s s s, t t t,问是否有一种区间反转的方案,使得这些区间没有相交,并且反转的区间数量最少。如果有就输出方案。

题解:
我们将两个字符串合成成 T = s 1 t 1 s 2 t 2 . . . s n t n T=s_1t_1s_2t_2...s_nt_n T=s1t1s2t2...sntn
那么问题就是最少要把整个字符串 T T T拆分成若干个偶数长度(并且长度大于2)的回文串。
长度是2的表示没有反转。
那么这个题就变成最小回文分解的裸题了。

d p [ i ] dp[i] dp[i]表示 s [ 1... i ] s[1...i] s[1...i]的最少反转次数。
p r e [ i ] pre[i] pre[i]表示在最优解里面i为区间右端点的左端点下标。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=1e9;
const int MAXN=1000004;
char s[MAXN];
struct PAT{
    int cnt,last;
    int ch[MAXN][26],len[MAXN],fa[MAXN],sz[MAXN];
    int slk[MAXN],diff[MAXN];
    int fp[MAXN];
    int create(int Len,int Fa){
        fa[cnt]=Fa;
        len[cnt]=Len;
        return cnt++;
    }
    void CLEAR(){
        for(int i=0;i<cnt;i++){
            fa[i]=0;len[i]=0;sz[i]=0;
            slk[i]=0;diff[i]=0;
            memset(ch[i],0,sizeof(ch[i]));
        }
        last=0;cnt=0;
        create(0,1);
        create(-1,0);
    }
    int getfail(int p,int n){
        for(;s[n-len[p]-1]!=s[n];p=fa[p]);
        return p;
    }
    int add(int c,int pos){
        int p=getfail(last,pos);
        if(!ch[p][c]){
            int nt=create(len[p]+2,ch[getfail(fa[p],pos)][c]);
            ch[p][c]=nt;
            diff[nt]=len[nt]-len[fa[nt]];
            slk[nt]=((diff[nt]==diff[fa[nt]])?slk[fa[nt]]:fa[nt]);
        }
        last=ch[p][c];
        return last;
    }
    void solve(int l,int *dp,int *pre){
        for(int i=0;i<=l;i++){
            pre[i]=0;
            dp[i]=INF;
        }
        CLEAR();
        dp[0]=0;
        fp[0]=1;
        for(int i=1;i<=l;i++){
            for(int j=add(s[i]-'a',i);j;j=slk[j]){
                fp[j]=i-len[slk[j]]-diff[j];
                if(diff[j]==diff[fa[j]]&&dp[fp[j]]>dp[fp[fa[j]]]){
                    fp[j]=fp[fa[j]];
                }
                if(i%2==0&&dp[i]>dp[fp[j]]+1){
                    dp[i]=dp[fp[j]]+1;
                    pre[i]=fp[j];
                }
            }
            if(i%2==0&&s[i-1]==s[i]&&dp[i]>=dp[i-2]){
                dp[i]=dp[i-2];
                pre[i]=i-2;
            }
        }
    }
}pat;
char ss[MAXN];
int dp[MAXN],pre[MAXN];
int main(){
    int n;
    scanf("%s",ss+1);
    n=strlen(ss+1);
    n<<=1;
    for(int i=1;i<=n;i+=2)s[i]=ss[i/2+1];
    scanf("%s",ss+1);
    for(int i=2;i<=n;i+=2)s[i]=ss[i/2];
    pat.solve(n,dp,pre);
    if(dp[n]>n)return puts("-1"),0;
    else printf("%d\n",dp[n]);
    for(int i=n;i;i=pre[i]){
        if(i-pre[i]>2)printf("%d %d\n",pre[i]/2+1,i/2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值