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. React Native中的远程调试是不可靠的

    一.原因 当您发现rn app在关闭远程调试后,一些功能无法正常工作时,这很可能是由于设备上的JavaScript执行环境与远程调试器之间的细微差别造成的. 例如,日期问题,Date构造函数似乎接受C ...

  2. RequireJS 是一个JavaScript模块加载器

    RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino and Node. 使用RequireJ ...

  3. javascript实现深克隆的几种方法

    1)普通函数实现 function cloneObject(obj) { if (obj === null || typeof obj !== 'object') { return obj; } va ...

  4. ubuntu桌面安装常用软件&及常见问题

    自己从windows转向ubuntu桌面开发,根据需求安装以下文件: ubuntu 桌面版下载:http://www.ubuntu.org.cn/download/desktop 有的公司设置静态ip ...

  5. eclipse 乱码问题总结

    Eclipse 的控制台必须用GBK编码.所以条件1和条件4必须同时满足否则运行的还是乱码.才能保证不是乱码. 条件1,Window  | Preferences  | Workspace  |  T ...

  6. minimal sparse ruler problem 最少尺子刻度问题

    一个长度13的尺子,如果在1位置刻点可以量出1和12,13三种刻度.那么至少刻几个点,可以直接量出1-13所有的长度,分别刻在哪几个位置? 注:必须是直接量.即在尺子上能找出一个1-13任意的整数长度 ...

  7. django定义Model中的方法和属性

    #定义一个Model class UserProfile(models.Model): user=models.OneToOneField(User,unique=True) phone=models ...

  8. VS2017配置cuda9.1编译不过问题。

    #if defined(_WIN32) #if _MSC_VER < 1600 || _MSC_VER > 1920 #error -- unsupported Microsoft Vis ...

  9. 探寻IIS最大并发数

    原文链接,http://www.cnblogs.com/birdshover/archive/2009/08/16/1547025.html 原文评论值得一看!   测试系统Window 2003 S ...

  10. stderr: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

    错误提示: (1). stderr: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer direc ...