• poj3461 Oulipo KMP算法


    http://poj.org/problem?id=3461

    题意:给两个字符串S,T;求S中最多含有多少个T。

    很显然直接贴个KMP算法就可以解决了。

    Source Code

    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    int next[10001],n;
    struct {
           char str[1000001];
           int len;
           }s,t;  //主串s, 子串t。
    void get_next()//求模式串的“next[]”值
    {
         long i=0,j=-1;
         next[0]=-1;
         while(i<t.len)
         {
           if(j==-1||t.str[i]==t.str[j])
             next[++i]=++j;
           else j=next[j];
         }
    }
    int kmp()
    {
        int ans=0,i=0,j=0;
        while(i<s.len)
        {
          if(j==-1||s.str[i]==t.str[j])
           { i++;j++; }
          else j=next[j];
          if(j==t.len)
          {
             ans++;
             j=next[j];    //要从j=next[j];重新开始算;
          }
        }
        return ans;
    }
         

    main()
    {
       long i,ans,T;
       scanf("%ld",&T);
       getchar();
       while(T--)
       { 
          gets(t.str);
          gets(s.str);
          t.len=strlen(t.str);
          s.len=strlen(s.str);
          get_next();
          printf("%ld\n",kmp());
       }
         
          system("pause");
    }

  • 相关阅读:
    pm2部署node应用
    koa源码分析
    将vim配置成一个轻量的IDE开发工具
    私有npm下载资源
    nodejs的会话总结
    redis---安全设置
    Lua----注意事项
    Leetcode538.-Convert BST to Greater Tree-Easy
    Leetcode 230-Kth Smallest Element in a BST-Medium
    Leetcode669-Trim a Binary Search Tree-Easy
  • 原文地址:https://www.cnblogs.com/zxj015/p/2740278.html
Copyright © 2020-2023  润新知