数据结构实验之串一:KMP简单应用

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

给定两个字符串string1和string2,判断string2是否为string1的子串。

Input

 输入包含多组数据,每组测试数据包含两行,第一行代表string1(长度小于1000000),第二行代表string2(长度小于1000000),string1和string2中保证不出现空格。

Output

 对于每组输入数据,若string2是string1的子串,则输出string2在string1中的位置,若不是,输出-1。

Example Input

abc
a
123456
45
abc
ddd

Example Output

1
4
-1

DQE:

 
KMP算法的经典应用,可自己推算一下或查阅相关资料。
 
 #include <iostream>
 #include <cstdio>

 using namespace std;

 int strlen(char *s)
 {
     ;
     while(s[i]!='\0')
     {
         i++;
     }
     return i;
 }

 void cnext(char *s,int *next)
 {
     int l=strlen(s);
     ,j=-;
     next[i]=j;
     )
     {
         ||s[i]==s[j])
         {
             i++;
             j++;
             next[i]=j;
         }
         else
         {
             j=next[j];
         }
     }
 }

 int kmp(char *s1,char *s2,int *next)
 {
     int l1=strlen(s1),l2=strlen(s2);
     ,j=;
     while(i<l1&&j<l2)
     {
         ||s1[i]==s2[j])
         {
             i++;
             j++;
         }
         else
         {
             j=next[j];
         }
     }
     if(j>=l2)
         ;
     ;
 }

 int main()
 {
     ],s2[];
     ];    //此处用static不完全等同于放到全局变量
     while(gets(s1))
     {
         gets(s2);
         cnext(s2,next);
         printf("%d\n",kmp(s1,s2,next));
     }
     ;
 }

 /***************************************************
 User name: ***
 Result: Accepted
 Take time: 68ms
 Take Memory: 1012KB
 Submit time: 2016-11-02 21:33:59
 ****************************************************/

SDUT 2772 数据结构实验之串一:KMP简单应用的更多相关文章

  1. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  2. SDUT OJ 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  3. SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解

    数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  4. SDUT OJ 数据结构实验之串二:字符串匹配

    数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  5. SDUT-3331_数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 有n个小朋友,每个小朋友手里有一些糖块, ...

  6. SDUT-2772_数据结构实验之串一:KMP简单应用

    数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定两个字符串string1和str ...

  7. SDUT 3401 数据结构实验之排序四:寻找大富翁.!

    数据结构实验之排序四:寻找大富翁 Time Limit: 150MS Memory Limit: 512KB Submit Statistic Problem Description 2015胡润全球 ...

  8. SDUT 3346 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  9. SDUT 3347 数据结构实验之数组三:快速转置

    数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...

随机推荐

  1. IntelliJ IDEA自动去掉行尾空格

    Settings→Editor→General 先选中Allow placement of caret after end of line 再修改Strip trailing spaces on Sa ...

  2. Hadoop:使用Mrjob框架编写MapReduce

    Mrjob简介 Mrjob是一个编写MapReduce任务的开源Python框架,它实际上对Hadoop Streaming的命令行进行了封装,因此接粗不到Hadoop的数据流命令行,使我们可以更轻松 ...

  3. update openssl on redhat/centos

    $ openssl versionOpenSSL 1.0.1e-fips 11 Feb 2013 $ yum list |grep opensslopenssl.x86_64 1.0.1e-16.el ...

  4. TX Textcontrol 使用总结五——添加图片

    实现如图所示效果: 实现代码如下所示: 注意,此处不做代码格式化处理... using System;using System.Collections.Generic;using System.Dra ...

  5. [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码

    转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...

  6. SVN分支的合并和同步

    使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及到分支的管理,也不敢贸然使用合并功能,生怕合并出了问题对团队造成不良影响,最主要的原因是,自己对分支的目的和合并 ...

  7. 【转】 远程到服务器安装visualSVN server,出现Service 'VisualSVN Server' failed to start的解决方法

    在帮助远程到服务器上安装visualSVN server的时候,出现Service 'VisualSVN Server' failed to start. 解决方法(先不要关闭安装弹出的错误窗口): ...

  8. Java执行main方法,异常为:could not find the main class.program will exit

    未解决. Java执行方法,异常为:could not find the main class.program will exitmain 原文地址:http://rogerfederer.iteye ...

  9. 单元测试(junit使用)

    1.测试的对象是一个类中的方法. 2.导入jar包. 3.单元测试方法时候,测试方法命名规则为 public void 方法名(){},注意:测试类不能命名为public class Test{},T ...

  10. php pcntl 多进程学习

    1.捕获子进程退出(监听SIGCHLD信号,然后调用 pcntl_wait 函数) declare(ticks=1); pcntl_signal(SIGCHLD, "sig_handler& ...