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

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. wordpress get_query_var()函数

    get_query_var函数的最主要作用就是能够查询得到当前文章的分类及分页.定义在:wp-includes/query.php 定义: function get_query_var($var) { ...

  2. openGL 变换06

    变换 使用(多个)矩阵(Matrix) 对象可以更好的变换(Transform)一个物体. 向量 向量最基本的定义就是一个方向. 或者说 向量有一个方向(Direction)和大小(Magnitude ...

  3. Codeforces 1109C 线段树

    题意及思路:https://www.cnblogs.com/TinyWong/p/10400682.html 代码: #include <bits/stdc++.h> #define ls ...

  4. mysql 错误总结 和FROM_UNIXTIME用法

    今天再倒入给数据库导入 .sql  文件的时候出现了一个错误.看错误码: You have an error in your SQL syntax; check the manual that cor ...

  5. 520. Detect Capital判断单词有效性

    [抄题]: Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  6. c语言实践 创建两个包含8个元素的double类型数组,第二个元素的每个元素的值都是对应前一个元素的前n个元素的和

    意思就是第二个元素的num[2]等于第一个元素的num[0]+num[1]+num[2] #define COUNT 8 int main(void) { double num1[COUNT]; do ...

  7. MCMC 破译密码 http://mlwhiz.com/blog/2015/08/21/MCMC_Algorithms_Cryptography/

    # AIM: To Decrypt a text using MCMC approach. i.e. find decryption key which we will call cipher fro ...

  8. python3--生成器并行运算

    # Auther: Aaron Fan """def consumer(name): print("%s 准备吃包子啦!" % name) while ...

  9. vue父子通信

    首先在组件创建中创建子组件Todos.vue <template> <div class="hello"> <h1>todos show< ...

  10. 通过javascript的日期对象来得到当前的日期

    var currentDate = new Date(); var weekday = ["星期日", "星期一", "星期二", &quo ...