SCU 4438 Censor(KMP / HASH)

过滤敏感词

其实就是找子串  就是记录答案的时候不一样

用一个类似于栈的容器记录已经匹配过的就行了   遇到匹配成功的出栈n个就行了

KMP 为了出栈后能继续之前的匹配  多一个数组记录栈中当前元素的失配位置   -  -

KMP:

#include 
using namespace std;
const int MAXN = 5e6+10;
struct KMP
{
    int f[MAXN];
    void getFail(char S[])
    {
        int i=0,j=-1;
        int len=strlen(S);
        f[0]=-1;
        while (i

HASH:

#include 
using namespace std;
typedef unsigned long long ULL;
const int MAXN  = 5e6+100;
const int SEED = 13331;

ULL ht[MAXN],hs;
ULL xl[MAXN];
char s[MAXN],t[MAXN],ans[MAXN];
int main()
{
    xl[0]=1;
    for(int i=1;i=n&&ht[top]-ht[top-n]*xl[n]==hs)
                top-=n;
        }
        for(int i=0;i


你可能感兴趣的:(......数据结构,............KMP,ACM)