刪除子串 KMP

2021-09-30 13:31:31 字數 1145 閱讀 2785

題目描述:

輸入的第一行是字串 s ,第二行是字串 t 。

輸出格式:

輸出最後的 s 串。資料保證 s 串不會為空串。

樣例輸入:

whatthemomooofun

moo

樣例輸出:

whatthefun

樣例說明

當列舉到 whatthemomoo 時,刪除 moo ,得到 whatthemo ;

繼續列舉 o ,得到 whatthemoo ,再次刪除 moo ,得到 whatthe ;

繼續列舉 fun ,最後得到 whatthefun 。

題目分析:

kmp。這道題的難點在於刪掉乙個t後,前後又可能出現一新的t串,並且怎樣」刪」也是乙個問題,怎樣才不會超時。最開始我把刪掉的部分賦成0,超時;把後面的往前移,超時。最後的方法是匹配的同時依次存入乙個陣列,每次刪掉乙個t時,此陣列下標就減掉乙個t的長度,相當於刪掉了t,然後繼續存。對於合併後可能產生新的t的問題,處理見**。

附**:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

const

int maxn=1e6+10;

int lens,lent,nxt[maxn],tot,pre[maxn];

char s[maxn],t[maxn],a[maxn];

int main()

for(int i=1,j=0;i<=lens;i++)

}for(int i=1;i<=tot;i++)

cout

0;}

KMP 演算法,search 子串

網上看了好多關於kmp演算法的,但是都是看的不清不楚的,用了好多術語,不明白,後來自己根據結果倒推了過程,不知對不對,暫時先記下來,kmp演算法,需要預先處理子串,然後建立乙個int型別next陣列 名字無所謂,主要是儲存一些關於子串的資訊 這裡有幾點要注意 next陣列計算方法 假定j為數字下標,...

KMP 子串查詢演算法

如何在目標字串s中,查詢是否存在子串p?樸素解法 字串s中查詢子串p的位置 int sub str index const char s,const char p ret equal i 1 return ret 樸素解法的乙個優化線索 因為,pa pb pc 且 pc sc 所以,pa sb pa...

FAFU OJ 刪除子串

刪除子串 time limit 1000ms memory limit 65536kb total submissions 624accepted 267 description 編寫程式實現從乙個字串str中,刪除其所有的子串sub。例如 str hyuaaaabcad dsj2390aaabca...