SDUT OJ 数据结构实验之串二:字符串匹配
数据结构实验之串二:字符串匹配
Problem Description
Input
Output
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 数据结构实验之串二:字符串匹配的更多相关文章
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解
数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之链表二:逆序建立链表
数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT 3341 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知二叉 ...
- SDUT 3311 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...
- SDUT 2772 数据结构实验之串一:KMP简单应用
数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
随机推荐
- wordpress get_query_var()函数
get_query_var函数的最主要作用就是能够查询得到当前文章的分类及分页.定义在:wp-includes/query.php 定义: function get_query_var($var) { ...
- openGL 变换06
变换 使用(多个)矩阵(Matrix) 对象可以更好的变换(Transform)一个物体. 向量 向量最基本的定义就是一个方向. 或者说 向量有一个方向(Direction)和大小(Magnitude ...
- Codeforces 1109C 线段树
题意及思路:https://www.cnblogs.com/TinyWong/p/10400682.html 代码: #include <bits/stdc++.h> #define ls ...
- mysql 错误总结 和FROM_UNIXTIME用法
今天再倒入给数据库导入 .sql 文件的时候出现了一个错误.看错误码: You have an error in your SQL syntax; check the manual that cor ...
- 520. Detect Capital判断单词有效性
[抄题]: Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- c语言实践 创建两个包含8个元素的double类型数组,第二个元素的每个元素的值都是对应前一个元素的前n个元素的和
意思就是第二个元素的num[2]等于第一个元素的num[0]+num[1]+num[2] #define COUNT 8 int main(void) { double num1[COUNT]; do ...
- 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 ...
- python3--生成器并行运算
# Auther: Aaron Fan """def consumer(name): print("%s 准备吃包子啦!" % name) while ...
- vue父子通信
首先在组件创建中创建子组件Todos.vue <template> <div class="hello"> <h1>todos show< ...
- 通过javascript的日期对象来得到当前的日期
var currentDate = new Date(); var weekday = ["星期日", "星期一", "星期二", &quo ...