• FZU 2122 又见LKity(KMP+返回所有匹配位置)


      基础kmp应用,找到所有匹配位置即可

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #include<iostream>
    using namespace std;
    char t[50005],f[110],aim[110];
    int mark[50005],next[50005];
    bool myequal(char a,char b)
    {
        if(a >= 'A' && a <= 'Z')
            a = a-'A'+'a';
        if(b >= 'A' && b<= 'Z')
            b = b-'A'+'a';
        if(a == b)return true;
        else return false;
    }
    void makenext(const char *p)
    {
        int q,k;
        int m = strlen(p);
        next[0] = 0;
        for(q = 1,k = 0;q < m;q++)
        {
            while(k > 0 && !myequal(p[q],p[k])) k = next[k-1];
            if(myequal(p[q],p[k])) k++;
            next[q] = k;
        }
    }
    void kmp(const char *t,const char *p)
    {
        int n,m,i,q;
        n = strlen(t),m = strlen(p);
        makenext(p);
        for(i = 0,q = 0;i < n;i++)
        {
            while(q > 0 && !myequal(p[q],t[i])) q = next[q-1];
            if(myequal(p[q],t[i])) q++;
            if(q == m) mark[i-m+1] = 1;
        }
    }
    int main()
    {
        while(gets(f))
        {
            gets(aim);
            gets(t);
            memset(mark,0,sizeof(mark));
            memset(next,0,sizeof(next));
            kmp(t,f);
            int lent = strlen(t),lenjump = strlen(f);
            for(int i = 0;i < lent;i++)
            {
                if(mark[i])
                {
                    cout<<aim;
                    i += lenjump-1;
                }
                else cout<<t[i];
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    spring-cloud Feign
    spring-cloud eureka注册发现
    spring-cloud zuul网关
    正则表达式整理
    nginx配置-Rewrite
    nginx配置-main
    nginx用途
    Java命令行命令
    Task
    JavaScript难点整理
  • 原文地址:https://www.cnblogs.com/jifahu/p/5448959.html
Copyright © 2020-2023  润新知