数据结构实验之串二:字符串匹配

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

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

 

Input

 输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。(string1和string2大小不超过100字符)

 

Output

 对于每组输入数据,若string2是string1的子串,则输出"YES",否则输出"NO"。

 

Sample Input

abc
a
123456
45
abc
ddd

Sample Output

YES
YES
NO

这依然是应用KMP算法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> char s1[1000005],s2[1000005];
int next[1000005];
void get_next(char s[1000005])
{
int i = 0;
int len = strlen(s);
next[0] = -1;
int j = -1;
while(i < len-1)
{
if( j == -1 || s[i] == s[j])
{
++i;
++j;
if( s[i] != s[j])
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
} int kmp(char *s,char *p)
{
int len1 = strlen(s);
int len2 = strlen(p);
int i = 0;
int j = 0;
while( i < len1 && j < len2)
{
if( j == -1 || s[i] == p[j])
{
i++;
j++;
}
else
j = next[j];
}
if( j >= len2)
return 1;
else
return 0;
}
int main()
{ while(scanf("%s",s1)!=EOF)
{
scanf("%s",s2);
get_next(s2);
if(kmp(s1,s2))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

SDUT OJ 数据结构实验之串二:字符串匹配的更多相关文章

  1. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

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

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

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

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

  4. SDUT OJ 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  5. SDUT OJ 数据结构实验之链表二:逆序建立链表

    数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  6. SDUT 3341 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...

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

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

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

    数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...

  9. SDUT 3399 数据结构实验之排序二:交换排序

    数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...

随机推荐

  1. Spring Cloud Eureka 2 (Eureka Server搭建服务注册中心)

    工具:IntelliJ IDEA 2017.1.2 x64.maven3.3.9 打开IDE  file===>new===>project next next 选择相应的依赖 next ...

  2. Location - BOM对象

    Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问. 例子 把用户 ...

  3. (二)在eclipse中使用maven

    二.配置Maven插件 2.1.配置使用的Maven

  4. 747. Largest Number At Least Twice of Others比所有数字都大两倍的最大数

    [抄题]: In a given integer array nums, there is always exactly one largest element. Find whether the l ...

  5. 一个虚拟机网络的XML描述

    <?xml version="1.0" encoding="utf-8"?> <VNET> <ID>1</ID> ...

  6. lucene 第一天

    Lucene/Solr   第一天 1. 课程计划 Lucene介绍 全文检索流程介绍 a) 索引流程 b) 搜索流程 Lucene入门程序 a) 索引实现 b) 搜索实现 分词器 a) 分词介绍 b ...

  7. ROS编译:catkin简析

    博客转载自:https://blog.csdn.net/zyh821351004/article/details/50388429 Catkin tutorials: http://wiki.ros. ...

  8. UOJ 176 新年的繁荣

    挺妙的解法. 发现边权很小,我们可以考虑从大到小枚举边权来进行$kruskal$算法,这样子对于每一个边权$i$,我们只要枚举$0 \leq j < m$,找到一个点使它的点权为$i | 2^j ...

  9. LoadRunner使用问题

    最近给客户做POC,为了测试大数据的框架的一个并发能力,使用loadrunner进行相关的测试,目前发现几个要注意的地方 1: loadrunner的Java脚本必须使用jdk1.6的32位版本 2: ...

  10. Claims Based Authentication and Token Based Authentication和WIF

    基于声明的认证方式,其最大特性是可传递(一方面是由授信的Issuer,即claims持有方,发送到你的应用上,注意信任是单向的.例如QQ集成登录,登录成功后,QQ会向你的应用发送claims.另一方面 ...