KMP入门题目

本文深入探讨了KMP算法的原理与实现,通过多个编程挑战题目,如HDU-1711、HDU-2087、HDU-3336等,详细解析了KMP算法在字符串匹配中的高效应用。文章涵盖了KMP算法的构造过程、时间复杂度分析及在不同场景下的优化策略。
摘要由CSDN通过智能技术生成

题目

HDU - 1711 Number Sequence

代码

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

/**
 * Created by Enzo Cotter on 2019/4/30.
 */

public class Main {
    public static int [] next = new int[10003];
    public static int n, m;
    public static int [] a = new int[1000004];
    public static int [] b = new int[10004];
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int T = input.nextInt();
        while (T-- > 0) {
            n = input.nextInt();
            m = input.nextInt();
            for (int i = 1; i <= n; i++)
                a[i] = input.nextInt();
            for (int i = 1; i <= m; i++)
                b[i] = input.nextInt();
            Arrays.fill(next, 0 );
            for (int i = 2, j = 0; i <= m; i++) {
                while (j > 0 && b[i] != b[j + 1]) j = next[j];
                if (b[i] == b[j + 1])
                    j++;
                next[i] = j;
            }
            int flag = -1;
            for (int i = 1, j = 0; i <= n; i++) {
                while (j > 0 && (j == m || a[i] != b[j + 1])) j = next[j];
                if (a[i] == b[j + 1])j++;
                if (j == m) {
                    flag = i - j + 1;
                    break;
                }
            }
            out.println(flag);
        }
        out.flush();
    }
}

题目

HDU - 2087 剪花布条

代码


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

/**
 * Created by Enzo Cotter on 2019/4/30.
 */

public class Main {
    public static int [] next = new int[10003];
    public static int n, m;
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        while (input.hasNext()) {
            String a = input.next();
            if (a.charAt(0)=='#')break;
            String b = input.next();
            n = a.length();
            m = b.length();
            Arrays.fill(next, 0 );
            for (int i = 2, j = 0; i <= m; i++) {
                while (j > 0 && b.charAt(i - 1) != b.charAt(j)) j = next[j];
                if (b.charAt(i - 1) == b.charAt(j))
                    j++;
                next[i] = j;
            }
            int cnt = 0;
            for (int i = 1, j = 0; i <= n; i++) {
                while (j > 0 && (j == m || a.charAt(i - 1) != b.charAt(j))) j = next[j];
                if (a.charAt(i - 1) == b.charAt(j))j++;
                if (j == m) {
                    cnt++;
                    j = 0;
                }
            }
            out.println(cnt);
        }
        out.flush();
    }
}

题目

HDU - 3336 Count the string

代码

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

/**
 * Created by Enzo Cotter on 2019/4/30.
 */

public class Main {
    public static int [] next = new int[200003];
    public static int n, m;
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int [] dp = new int[200004];
        int t = input.nextInt();
        while (t-- > 0) {
            int n = input.nextInt();
            String s = input.next();
            Arrays.fill(next, 0);
            Arrays.fill(dp,1);
            for (int i = 2, j = 0; i <= n; i++) {
                while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
                if (s.charAt(i - 1) == s.charAt(j))j++;
                next[i] = j;
            }
            for (int i = 1; i <= n; i++) {
                dp[next[i]]++;
            }
            long sum = 0;
            for (int i = 1; i <= n; i++)
                sum = (sum + dp[i])%10007;
            out.println(sum);
        }
        out.flush();
    }
}

题目

HDU - 3746 Cyclic Nacklace

题意

  求最少需要添加字符的个数,使得整个串可以表示为某个串循环得到的;

代码

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
 
const int N = 1000002;
int nxt[N];
char  T[N];
int  tlen;
 
void getNext()
{
    int j, k;
    j = 0; k = -1;
    nxt[0] = -1;
    while(j < tlen)
        if(k == -1 || T[j] == T[k])
           {
               nxt[++j] = ++k;
               if (T[j] != T[k]) //се╩╞
                nxt[j] = k; 
           } 
        else
            k = nxt[k];
 
}
 
 
int main()
{
 
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%s",&T);
        tlen= strlen(T);
        
        getNext();
        if(nxt[tlen]==0)
        {
        printf("%d\n",tlen) ;
        continue;
        }
      
           if(tlen%(tlen-nxt[tlen])==0)
           printf("0\n");
        else
        {
            
            printf("%d\n",tlen-nxt[tlen]-tlen%(tlen-nxt[tlen]));
        }     
            
    }
    return 0;
}

题目

POJ - 3461 Oulipo

题意

  求子串与主串匹配的次数,可以重叠

代码

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

/**
 * Created by Enzo Cotter on 2019/4/30.
 */

public class Main {
    public static int [] next = new int[10003];
    public static int n, m;
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int T = input.nextInt();
        while (T-- > 0) {
            String b = input.next(), a = input.next();
            n = a.length();
            m = b.length();
            Arrays.fill(next, 0 );
            for (int i = 2, j = 0; i <= m; i++) {
                while (j > 0 && b.charAt(i - 1) != b.charAt(j)) j = next[j];
                if (b.charAt(i - 1) == b.charAt(j))
                    j++;
                next[i] = j;
            }
            int cnt = 0;
            for (int i = 1, j = 0; i <= n; i++) {
                while (j > 0 && (j == m || a.charAt(i - 1) != b.charAt(j))) j = next[j];
                if (a.charAt(i - 1) == b.charAt(j))j++;
                if (j == m) {
                    cnt++;
                    j = next[j];
                }
            }
            out.println(cnt);
        }
        out.flush();
    }
}

题目

POJ -1961 Period

题意

  对于一个字符串的所有前缀,如果整个前缀可以表示为某个子串重复 k k k次,就输出整个串的长度及这个k;

代码


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

/**
 * Created by Enzo Cotter on 2019/4/30.
 */

public class Main {
    public static int [] next = new int[1000003];
    public static int n, m;
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int cnt = 0;
        while (input.hasNext()) {
            int n = input.nextInt();
            if (n == 0)break;
            out.println("Test case #" + ++cnt);
            String s = input.next();
            Arrays.fill(next, 0);
            for (int i = 2, j = 0; i <= n; i++) {
                while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
                if (s.charAt(i - 1) == s.charAt(j))j++;
                next[i] = j;
            }
            for (int i = 2; i <= n; i++) {
                if (next[i] == 0)continue;
                if (i % (i - next[i]) == 0) {
                    out.println(i + " " + i / (i - next[i]));
                }
            }
            out.println();
        }
        out.flush();
    }
}

题目

POJ -2406 Power Strings

题意

  对于给定的字符串 s s s s s s可以表示为 a n a^n an,输出这个最大的 n n n

代码

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;

public class Main {
    public static int [] next = new int[1000003];
    public static int n, m;
    public static void main(String[] args) {
        Scanner input = new Scanner(new BufferedInputStream(System.in));
        PrintWriter out = new PrintWriter(System.out);
        while (input.hasNext()) {
            String s = input.next();
            if (s.equals("."))break;
            n = s.length();
            Arrays.fill(next, 0);
            for (int i = 2, j = 0; i <= n; i++) {
                while (j > 0 && s.charAt(i - 1) != s.charAt(j)) j = next[j];
                if (s.charAt(i - 1) == s.charAt(j))j++;
                next[i] = j;
            }
            if (n % (n - next[n]) ==0)
                out.println(n / (n - next[n]));
            else out.println(1);
        }
        out.flush();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值