Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 226     Solved: 189


Description

给定两个字符串s和t,现有一个扫描器,从s的最左边开始向右扫描,每次扫描到一个t就把这一段删除,输出能发现t的个数。

Input

第一行包含一个整数T(T<=50),表示数据组数。
每组数据第一行包含一个字符串s,第二行一个字符串t,字符串长度不超过1000000。

Output

对于每组数据,输出答案。

Sample Input

2
ababab
ab
ababab
ba

Sample Output

3
2

Hint

Source

国防科学技术大学第十八届银河之光文化节ACM程序设计竞赛初赛

 
分析:
直接暴力匹配或者KMP
KMP:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
char moban[],wenben[];
int next1[];
int sum;
void getnext(char* s,int* next1,int m)
{
next1[]=;
next1[]=;
for(int i=;i<m;i++)
{
int j=next1[i];
while(j&&s[i]!=s[j])
j=next1[j];
if(s[i]==s[j])
next1[i+]=j+;
else
next1[i+]=;
}
}
void kmp(char* ss,char* s,int* next1,int n,int m)
{
int ans=;
getnext(s,next1,m);
int j=;
for(int i=;i<n;i++)
{
while(j&&s[j]!=ss[i])
j=next1[j];
if(s[j]==ss[i])
j++;
if(j==m)
{
ans++;
j=;//注意复位!!!!
}
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",wenben);
scanf("%s",moban);
int n=strlen(wenben);
int m=strlen(moban);
kmp(wenben,moban,next1,n,m);
}
return ;
}

暴力:

#include<stdio.h>
#include<string.h>
using namespace std;
#define max_v 1005
char s1[max_v],s2[max_v];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s1);
scanf("%s",s2);
int l1=strlen(s1);
int l2=strlen(s2);
int sum=;
int j=;
for(int i=; i<l1; i++)
{
if(s1[i]==s2[j])
{
j++;
}
else
{
j=;
}
if(j==l2)
{
sum++;
j=;
}
}
printf("%d\n",sum);
}
return ;
}

CSU 1598 最长公共前缀 (简单KMP或者暴力)的更多相关文章

  1. LeetCode刷题-最长公共前缀(简单)

    题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  2. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. 【leetcode 简单】第五题 最长公共前缀

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  4. 【Leetcode】【简单】【14最长公共前缀】【JavaScript】

    题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

  5. 【leetcode算法-简单】14. 最长公共前缀

    [题目描述] 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","fl ...

  6. 14. Longest Common Prefix[E]最长公共前缀

    题目 Write a function to find the longest common prefix string amongst an array of strings. If there i ...

  7. 领扣(LeetCode)最长公共前缀 个人题解

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  8. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  9. python刷LeetCode:14. 最长公共前缀

    难度等级:简单 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...

随机推荐

  1. SQLite metadata

    http://www.devart.com/dotconnect/sqlite/docs/MetaData.html https://github.com/sqlitebrowser/sqlitebr ...

  2. js 控制页面跳转的5种方法

    js 控制页面跳转的5种方法 编程式导航: 点击跳转路由,称编程式导航,用js编写代码跳转. History是bom中的 History.back是回退一页 Histiory.go(1)前进一页 Hi ...

  3. 修改Nginx 伪静态Rewrite规则 安装Chevereto

    Chevereto 是目前最为强大的 PHP 图床系统,通过它可部署多用户公开或私有的图片存储服务,现在 Chevereto 出了免费的版本,小伙伴可以围观一下. https://github.com ...

  4. jQuery轮播图(二)利用构造函数和原型创建对象以实现继承

    本文是在我开始学习JavaScript继承时,对原型继承的一些理解和运用.文中所述的继承方式均是使用js特有的原型链方式,实际上有了ES6的类之后,实现继承的就变得十分简单了,所以这种写法现在也不在推 ...

  5. LeetCode 1. Two Sum (JavaScript)

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. maven(12),排除冲突JAR包

     JAR包冲突 <dependencies> <dependency> <groupId>org.springframework</groupId> ...

  7. Windows系统环境下安装dlib

    Windows系统环境下安装dlib 因为今天需要在windows环境下做一些图片处理,所以需要在pycharm中配置环境,而其中需要的主要是dlib的安装: 下面说一下关于dlib的配置安装: —- ...

  8. 找工作笔试面试那些事儿(13)---操作系统常考知识点总结 ZZ 【操作系统】

    http://blog.csdn.net/han_xiaoyang/article/details/11285485 上一节对数据库的知识做了一个小总结,实际找工作过程中,因为公司或单位侧重点不一样, ...

  9. 运维url收集

    https://www.centos.bz/tag/nagios/ Graphite的百万Metrics实践之路 饿了么 Influxdb 实践之路

  10. Jmeter和LoadRunner的区别

    1.Jmeter的架构跟LoadRunner原理一样,都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务器反馈的结果的一个过程. 2.分布式中间代 ...