惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

博客园 - I love I think

Tomcat 配置集锦 eclipse+tomcat python的对象与名字绑定(转贴,此文甚好) 单引号、双引号和三双引号的区别 - I love I think 这几天一直在看Latex,试着排版了一份试卷。 开机即打开Numlock - I love I think Keep track of own postings using Summary Highlighting (Submitted by dzimmerm on Sun, 12/28/2003 - 18:31. ) 哈夫曼树(调试没通过,郁闷) 新闻组上看到一篇关于linux内核编译的文章,留待日后参考。 调整显示屏幕偏移和刷新频率[zt] The best way to predict future is to invent it.------Alan Kay 数学学科分类标准 有趣的东西太多了。 nvidia驱动安装 linux内核源码目录结构 Linux打印介绍【转贴】 矩阵的相似对角化到底有什么用?该不会是纯粹的理论吧? 宫崎骏的动画片真有意思啊。 elisp1
KMP算法
I love I think · 2004-12-25 · via 博客园 - I love I think

#include<iostream>
#include<time.h>
#include<string>
using namespace std;
void init(string ,string);
void show(char [],int);
int kmp(string ,string,int pos);
void get_next(char*,int *);
string s1,t1;
int m,n;
char *s;
char *t;
int *next;
/*************************MAIN**************************************/
int main(int argc[],char*args[])
{
   
 double t=clock();
 cout<<"找到位置为:"<<kmp("acbsabcaacabaabaabcacaabc","abaabca",1)<<endl;
    delete[] s;
 delete[] next;
 cout<<"耗时:"<<(clock()-t)/1000<<"毫秒!"<<endl;
 return 0;
}  
/**********************++++NEXT++++********************************/
void get_next(char s[],int ne[])
{
    ne =new int[n+1];
    next=new int[n+1];
    ne[0]=9999;
    int i(1),j(0);
    ne[1]=0;
    while(i<=(int)(t[0]))//数组是字符型的,要转化为整数
 {
     if(j==0||t[i]==t[j]){++i;++j;ne[i]=j;}
     else j=ne[j];
 }
   for( i=1;i<=n;i++)
   {
   cout<<"next["<<i<<"]="<<ne[i]<<endl;
      next[i]=ne[i];
   }
}
/********************++++KMP+++**********************************/
  int kmp(string s0,string t0,int pos)
  {
        init(s0,t0);
        int i=pos,j=1;
        while(i<=((int)(s[0]))&&(j<=((int)(t[0]))))
  {
         if((j==0)||(s[i]==t[j])){++i;++j;}
      else j=next[j];
        
  }
        if(j>(int)(t[0])) return i-((int)(t[0]));
        else return 0;

  }
/***************++++INIT+++*****************************************/
    void init(string ss,string tt)
 {
       //cout<<"请输入原串S=: "<<endl;
       //cin>>s1;
       //cout<<"请输入模式串T=:"<<endl;
       //cin>>t1;
       s1=ss;
       t1=tt;
       m=s1.length();
       n=t1.length();
       //if((s=(char*)malloc((m+1)*sizeof(char)))<0){cout<<"failed\n";return;}
       s=new char[m+1];
       s[0]=m;
       //if((t=(char*) malloc((n+1)*sizeof(char)))<0) {cout<<"failed\n";return;}
       t=new char[n+1];
       t[0]=n;
       for(int i=1;i<=m;i++)
      s[i]=s1.at(i-1);
       for( i=1;i<=n;i++)
         t[i]=t1.at(i-1);
        cout<<"原串为: ";    show(s,m);
     cout<<"模式串为: ";  show(t,n);
        get_next(t,next);
 }
/*******************++++SHOW+++**************************************/
       void show(char s[],int n )
    {
         for(int i=1;i<=n;i++)
         cout<<s[i]<<"  ";
         cout<<endl;
         cout<<"长度为: "<<int(s[0])<<"\n";
    }

posted on 2004-12-25 02:55  I love I think  阅读(3762)  评论()    收藏  举报