题目大意:首先给你第一串字符串(s1),让你删到一些字母,变成下面的字符串(s2),删除的字母会变成字符‘_’;
思路:用kmp算法让s1每一个单词在s1上匹配,从前面开始找,记得匹配成功过,i要跳一个位置,因为单词之间是有空格的,找到一个单词后,你要空一格。
kmp算法 具体参考:https://blog.csdn.net/dark_cy/article/details/88698736讲的超级好
代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <iomanip>
#include <sstream>
#include <set>
#include <map>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define rep(i,n) for(int i=0;(i)<(n);i++)
#define rep1(i,n) for(int i=1;(i)<=(n);i++)
#define se secondusing namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > pii;
const ll mod=10001;
const ll N =1e5+10;
const double esp = 1e-6;
//const double pi=acos(-1);
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
//void exgcd(int a,int b,int& x,int& y){if(b == 0){x = 1 ;y = 0 ;return ;}exgcd(b,a%b,y,x);y-=x*(a/b);}
int dx[4]= {-1,0,1,0}, dy[4] = {0,1,0,-1};
int next1[N];
int dp[N];
int dp1[N];
int sum[N];
void get_next(string t)
{int j=0,k=-1;next1[0]=-1;while(j<t.length()-1){if(k == -1 || t[j] == t[k]){j++;k++;next1[j] = k;}else k = next1[k];}
}void solve()
{int idx=0;string s,a,b;getline(cin,s);getline(cin,a);stringstream ss(a);int i=-1;bool f=true;while(ss>>b){i++;//跳一格get_next(b);//cout<<b<<endl;int len=b.length();//for(int i=0;i<=len;i++) cout<<next[i]<<endl;int j=0;while(i<s.length()&&j<len)//匹配过程{if(j==-1 || s[i]==b[j]){i++;j++;}else j=next1[j];}if(j>=len){dp[idx]=i-len;//单词的第一个位置dp1[idx++]=i-1;//单词的最后一个位置//cout<<1<<endl;}else{f=false;//没有找到break;}}if(!f){puts("I HAVE FAILED!!!");return;}int x=0;for(int i=0;i<s.length();i++)//标记 将单词的位置标记一下,没有标记的字母变成'_'{if(i==dp[x]){for(;i<=dp1[x];i++){sum[i]=1;}i-=1;x++;}}for(int i=0;i<s.length();i++)//输出{if(sum[i]==1) cout<<s[i];elseif(s[i]==' ') cout<<" ";else{cout<<"_";}}}
int main()
{iosint T;//cin>>T;T=1;while(T--){solve();}return 0;
}