题目链接:https://ac.nowcoder.com/acm/contest/6760/B
Description
另一天,一只可爱的围着围巾的肥企鹅在路上摇摇晃晃地走着,遇上了迎面走来的打着饱嗝的PM6。小企鹅预感不妙,这不就是最近有名的恶人PM6么!吓得立刻扭头就想跑。
PM6:“小火汁,站住!我不吃你(谁叫你是保护动物)。我这有一道简单题,如果你答对了,我就给你吃鱼肉,如果你答错了,就免费帮我充游戏币!”
企鹅:“(:3J∠)(默默摘掉围巾)”
PM6:“我给你一个文本串 S ,再给你两个串A、B,你要将文本串中的 A 都转换成 B ,转换后的字符不再参与转换,输出最终的文本串。”
求求你救救企鹅!
Input
第一行输入一个文本串 S 。
第二行输入字符串 A 。
第三行输入字符串 B 。
|S|为S的长度,|A|为A的长度,|B|为B的长度,所有字符都是小写字母,保证 |A| <= |S| 。
对于50%的数据:1<= |A|、|B|、|S| <=1000
对于100%的数据:1<= |A|、|B|、|S| <=1000000
Output
只有一行,输出转换后的文本串。
Sample Input
abababcd
ab
cd
Sample Output
cdcdcdcd
Solution
明显的KMP模板题
AC Code
#include <iostream>
#include <cstring>
#include<string>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
/* P 为模式串,下标从 0 开始 */
void GetNext(string P, int next[])
{
int p_len = P.size();
int i = 0; // P 的下标
int j = -1; //设成-1,可以在匹配不上的时候,并且已经回溯到了第一个字符时,作为边界条件,然后模式串会整体右移一个位置
next[0] = -1;
while (i < p_len)
{
if (j == -1 || P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
/* 在 S 中找到 P 第一次出现的位置 */
int KMP(string S, string P, int next[])
{
GetNext(P, next);
int i = 0; // S 的下标
int j = 0; // P 的下标
int s_len = S.size();
int p_len = P.size();
while (i < s_len && j < p_len)
{
if (j == -1 || S[i] == P[j]) // P 的第一个字符不匹配或 S[i] == P[j]
{
i++;
j++;
}
else
j = next[j]; // 当前字符匹配失败,进行跳转
}
if (j == p_len) // 匹配成功
return i - j;
return -1;
}
int Next[1000010] = { 0 }; //next数组的含义就是一个固定字符串的最长前缀和最长后缀相同的长度
int main()
{
string s1, s2,s3;
cin >> s1 >> s2>>s3;
int len=s2.length();
int pos;
pos = KMP(s1, s2, Next);
while(pos!=-1){
s1.replace(pos,len,s3);
pos = KMP(s1, s2, Next);
}
cout<<s1<<endl;
}