Ural 1684. Jack's Last Word 多串KMP

1684. Jack's Last Word

Time limit: 0.5 second
Memory limit: 64 MB
Not long ago Jack read on a fence a word that was new and amusing for him. Jack liked it so much that he wanted to invent another amusing word, but no matter how hard he tried he couldn't do it. All the words he could invent consisted of prefixes of the initial word and therefore didn't please Jack. He continued to invent words that were longer and longer, but none of them was pleasing.
And now the time has come for Jack to have his last word.

Input

The first line contains the amusing word that was written on the fence. The second line contains Jack's last word. The words are nonempty, consist of lower-case English letters, and are no longer than 75000 symbols.

Output

If Jack didn't invent a new amusing word, output “No” in the first line. In this case, show Jack how to decompose his last word into several parts each of which is either the initial word or its nonempty prefix. Output these parts in the second line separating them with a space. If there is no such decomposition, output “Yes” in the only output line.

Samples

input output
abracadabra
abrabracada
No
abr abracad a
abracadabra
arbadacarba
Yes
Problem Author: Dmitry Ivankov
Problem Source: USU Open Personal Contest 2009 (February 28, 2009)
/** 2013_7_27
 *
 * Ural 1684. Jack's Last Word
 * 给两个字符串a b,问你b是否可拆分成若干个部分,使得每个部分都是a的前缀.
 * KMP的应用
 *
 */
#include <iostream>
#include <string>
using namespace std;
const int maxn=75002;
int n,m,tot,anext[maxn],bnext[maxn];
string a,b,c[maxn];
int main()
{
    int i,j,num;
    cin>>a>>b;
    n=a.length();
    m=b.length();
    a=" "+a;
    b=" "+b;
    for (j=anext[1]=0,i=2; i<=n; ++i)
    {
        while (j && a[j+1] != a[i])
            j=anext[j];
        if (a[j+1] == a[i])
            j++;
        anext[i]=j;
    }
    for (j=0,i=1; i<=m; ++i)
    {
        while (j && a[j+1] != b[i])
            j=anext[j];
        if (a[j+1] == b[i])
            j++;
        bnext[i]=j;
    }
    num=-1;
    for (i=m; i&&bnext[i]; i-=bnext[i])
    {
        num++;
        for (j=i-bnext[i]+1; j<=i; j++)
            c[num]+=b[j];
    }
    if (i)
        cout<<"Yes"<<endl;
    else
    {
        cout<<"No"<<endl;
        for(; num; num--)
            cout<<c[num]<<" ";
        cout<<c[0]<<endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值