1204:寻找子串位置

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 18K  Solved: 8K

Description

给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置。

Input

仅一行包含两个字符串a和b

字符串的长度均不超过100

Pascal用户请注意:两个字符串之间可能包含多个空格

Output

仅一行一个整数

Sample Input

abcd bc

Sample Output

2

HINT

题解:

这就是传说中的傻逼暴力题!

但是,我会传说中的KMP怎么破?

O(∩_∩)O哈哈哈~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
#define eps 1e-9
const int inf=0x7fffffff; //无限大
void kmp_pre(char x[],int m,int next[])
{
int i,j;
j=next[]=-;
i=;
while(i<m)
{
while(-!=j&&x[i]!=x[j])j=next[j];
next[++i]=++j;
}
}
int next[maxn];
int Kmp_count(char x[],int m,char y[],int n)
{
int i,j;
int ans=;
//preKmp(x,m,next);
kmp_pre(x,m,next);
i=j=;
while(i<n)
{
while(-!=j&&y[i]!=x[j])j=next[j];
i++;j++;
if(j>=m)
{
return i;
ans++;
j=next[j];
}
}
}
int main()
{ char a[maxn];
char b[];
scanf("%s%s",a,b);
int n=strlen(a);
int m=strlen(b);
printf("%d\n",Kmp_count(b,m,a,n)-m+);
}

codevs 1204 寻找子串位置 KMP的更多相关文章

  1. codevs 1204 寻找子串位置

    http://codevs.cn/problem/1204/ 1204 寻找子串位置  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  2. wikioi 1204 寻找子串位置

    /*======================================================================== 1204 寻找子串位置 题目描述 Descript ...

  3. C++之路进阶——codevs1204(寻找子串位置)

    1204 寻找子串位置  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze     题目描述 Description 给出字符串a和字符串b,保证b是a的一个子 ...

  4. 寻找子串位置 codevs 1204

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述  ...

  5. 寻找子串位置<codevs>

    KMP板子题; 如果不会可以参考其他算法书 代码: #include<iostream> #include<stdio.h> #include<stdlib.h> ...

  6. codevs1204 寻找子串位置

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述  ...

  7. CODE[VS]-寻找子串位置-字符串处理-天梯青铜

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...

  8. 匈牙利算法 codevs 2776 寻找代表元

    codevs 2776 寻找代表元  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description 广州二中苏元实验学校一共有n个社团,分别用 ...

  9. 【CodeVS】1204 寻找字串位置

    题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述  ...

随机推荐

  1. centos 安装memcache服务后memcahce本机连接Permission

    自己手动在虚拟机下装了下memcache,整个过程真是充满波折,本身用php5.3安装memcache扩展就麻烦很多,无法通过yum直接安装,安装方法详见http://chenwei.me/blog/ ...

  2. 音频增益响度分析 ReplayGain 附完整C代码示例【转】

    转自:http://www.cnblogs.com/cpuimage/p/8846951.html 人们所熟知的图像方面的3A算法有: AF自动对焦(Automatic Focus)自动对焦即调节摄像 ...

  3. 事件,使用.net自带委托EventHandler

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. ajax.BeginForm异步提交表单并显示更新数据

    view代码: <!--基本信息模块--> 2 <div class="profile_box" id="basicInfo"> 3 & ...

  5. WPF Devexpress GridControl Value与Display转换

    直入主题吧!开发中往往需要将代码转换成中文显示在表格中. 如下图 下面就直接贴代码了. C#代码 using System; using System.Collections.Generic; usi ...

  6. java 读取配置文件类

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; im ...

  7. 13 JSON-RPC: a tale of interfaces

    JSON-RPC: a tale of interfaces 27 April 2010 Here we present an example where Go's interfaces made i ...

  8. 栈应用之 括号匹配问题(Python 版)

    栈应用之 括号匹配问题(Python 版) 检查括号是否闭合 循序扫描被检查正文(一个字符)里的一个个字符 检查中跳过无关字符(所有非括号字符都与当前处理无关) 遇到开括号将其压入栈 遇到闭括号时弹出 ...

  9. python基础--os模块和sys模块

    os模块提供对操作系统进行调用的接口 # -*- coding:utf-8 -*-__author__ = 'shisanjun' import os print(os.getcwd())#获取当前工 ...

  10. VS2015的对象浏览器的使用

    用vs开发这么久了,还是第一次用上对象浏览器的功能,第一次用有一点懵逼,记录一下. 这个图标是项目 这是代表类,下面可以展开看到基类 在右边可以看到这个类的方法和成员 这个代表结构体 同样的右边显示成 ...