1684. Jack's Last Word
Time limit: 0.5 second
Memory limit: 64 MB
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)
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;
}