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

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结合axis开发webservice

    第一步:引入axis的依赖jar包  第二步:修改web.xml文件  <listener> <listener-class>org.apache.axis.transport ...

  2. iPython notebook 安装使用

    pip install jupyter jupyter notebook --allow-root

  3. zookeeper更进一步(数据模型、watcher及shell命令)

    ZooKeeper数据模型 ZooKeeper 的数据模型,在结构上和标准文件系统的非常相似,拥有一个层次的命名空间,都是采用树形层次结构,ZooKeeper 树中的每个节点被称为—Znode.和文件 ...

  4. CronTrigger表达式和Quartz使用实例入门

    CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,如每月8日发工资的定时任务. CronTrigger,你可以 ...

  5. c++多线程编程(三)

    关于WaitForSingleObject函数用法 : 用户模式的线程同步机制效率高,如果需要考虑线程同步问题,应该首先考虑用户模式的线程同步方法. 但是,用户模式的线程同步有限制,对于多个进程之间的 ...

  6. 重命名Docker容器

    重命名Docker容器: Docker rename [Old container name]  [New container name]

  7. algorithm notes

    1.算法可视化 https://visualgo.net/en

  8. 调用req.getParameter方法出现中文乱码(全是问号???)

    在java开发中,如果编码配置不统一,很容易出现中文乱码的情况,这里就记录下自己遇到的调用req.getParameter方法出现中文乱码,并解决这一情况的方法 注意修改以下几个地方 1.jsp页面中 ...

  9. 用js获取cookie

    //获取cookiefunction cookie_email(cookie_name){ var cookiestr = document.cookie; if (cookiestr.length ...

  10. Open Message Queue 集群问题

    nohup ./imqbrokerd -tty -name myBroker -port 7677 -javahome /opt/omae/jdk1.7.0_45 -cluster 192.168.2 ...