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

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. Struts2处理逻辑的方式

    1.可以统一写一个action 对应方法名处理不同逻辑 2.也可以分别写Action 分别处理不同的逻辑

  2. Reading RxJava Marble Diagrams

    ------>表示一个Observable(承时间推移,由左入右,左边item先发射) ------>上面的图形,表示这个Observable发射的item ------>上的的|( ...

  3. MySQL5.7 在CentOS 下的安装

    尝试了在版本的CentOS6.6 和CentOS7.2 下安装,在6.6下比较复杂些.特地做下记录 在CentOS7.2 下安装,需要在官网下载 mysql-5.7.16-1.el7.x86_64.r ...

  4. Centos7.2 下搭建LNMP环境(终极版)Yum安装

    PHP7.1+Nginx+MySQL5.7 安装PHP //安装源只要遇到选择的全是Y rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-rele ...

  5. Python05 函数

    待更新... 2018-4-16 09:00:30

  6. 535. Encode and Decode TinyURL 长短URL

    [抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...

  7. 面试题:String StringBufere StringBuilder 不用看

    一.String 使用 private final char value[]来实现字符串存储 所以String对象创建之后就不能再修改此对象中存储的字符串内容,所以说String本质是字符数组char ...

  8. Luogu 1580 [NOIP2016] 换教室

    先用Floyed做亮点之间的最短路,设计dp,记dp[i][j][0]为到第i节课,换了j次课,当前有没有换课达到的期望耗费体力最小值 方程(太长了还是看代码吧):dp[i][j][0]<-dp ...

  9. Entity Framework 6.0 Tutorials(5):Command Interception

    Interception: Here, you will learn how to intercept EF when it executes database commands. EF 6 prov ...

  10. hdu 1905 Pseudoprime numbers

    #include<stdio.h> #include<math.h> #define ll long long ll mod; bool Judge(int x) { ;i&l ...