时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1259

解决:686

题目描述:

Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs. 

    Typically,the text is a document being edited,and the pattern searched for is a particular word supplied by the user.  

    We assume that the text is an array T[1..n] of length n and that the pattern is an array P[1..m] of length m<=n.We further assume that the elements of P and  T are all alphabets(∑={a,b...,z}).The character arrays P and T are often called strings of characters.  

    We say that pattern P occurs with shift s in the text T if 0<=s<=n and T[s+1..s+m] = P[1..m](that is if T[s+j]=P[j],for 1<=j<=m).  

    If P occurs with shift s in T,then we call s a valid shift;otherwise,we calls a invalid shift. 

    Your task is to calculate the number of vald shifts for the given text T and p attern P.

输入:

For each case, there are two strings T and P on a line,separated by a single space.You may assume both the length of T and P will not exceed 10^6.

输出:

You should output a number on a separate line,which indicates the number of valid shifts for the given text T and pattern P.

样例输入:
abababab abab
样例输出:
3
来源:
2006年上海交通大学计算机研究生机试真题

思路:

简单的计数题。

代码:

#include <stdio.h>
#include <string.h> #define N 1000000 int main(void)
{
int tlen, plen, i;
char t[N+1], p[N+1]; while (scanf("%s%s", t, p) != EOF)
{
tlen = strlen(t);
plen = strlen(p);
int count = 0;
for(i=0; i<=tlen-plen; i++)
{
if (t[i] == p[0] && strncmp(t+i, p, plen) == 0)
count ++;
}
printf("%d\n", count);
} return 0;
}
/**************************************************************
Problem: 1094
User: liangrx06
Language: C
Result: Accepted
Time:30 ms
Memory:2788 kb
****************************************************************/

九度OJ 1094:String Matching(字符串匹配) (计数)的更多相关文章

  1. 九度oj题目1165:字符串匹配

    题目1165:字符串匹配 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3078 解决:1079 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中 ...

  2. 九度OJ 1021:统计字符 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5418 解决:3146 题目描述:     统计一个给定字符串中指定的字符出现的次数. 输入:     测试输入包含若干测试用例,每个测试用 ...

  3. 九度OJ 1199:找位置 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2083 解决:1010 题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1:a,4 ...

  4. 九度OJ 1182:统计单词 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3920 解决:1443 题目描述: 编一个程序,读入用户输入的,以"."结尾的一行文字,统计一共有多少个单词,并分别输出 ...

  5. 九度OJ 1149:子串计算 (计数、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1026 解决:571 题目描述: 给出一个01字符串(长度不超过100),求其每一个子串出现的次数. 输入: 输入包含多行,每行一个字符串. ...

  6. 九度OJ 1098:字母统计 (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3720 解决:1809 题目描述: 输入一行字符串,计算其中A-Z大写字母出现的次数 输入: 案例可能有多组,每个案例输入为一行字符串. 输 ...

  7. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  8. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  9. 【九度OJ】题目1054:字符串内排序 解题报告

    [九度OJ]题目1054:字符串内排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1054 题目描述: 输入 ...

随机推荐

  1. BZOJ 4077 Messenger

    Messenger [问题描述] alice和bob各自在两条折线上行进,一个邮递员要从alice那拿一个包裹,并以直线移动到bob处,alice和bob.邮递员的速度均为1单位/s,问邮递员最少要走 ...

  2. app_data中的数据库使用

    原文发布时间为:2008-07-24 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET中利用VWD操作数据库 建立本地数据库   你可以轻易地在Visual Studio的Web应用程序 ...

  3. IDEA重新打jar包时报错MANIFEST.MF already exists in VFS

    报错原因:曾经打过jar包了,把之前的包删掉无用,VFS:虚拟文件系统.即使删掉之前的包,信息依然会在此处.故删掉MANIFEST文件夹,重新打包即可解决.

  4. Java中获取ServletContext的方法

    Servlet: this.getServletContext() this.getServletConfig().getServletContext() request.getSession().g ...

  5. Objective-C 的 API 设计(转)

    英文原文:API Design 转自oschina 参与翻译(14人): 李远超, 魏涛, showme, weizhe72, 周荣冰, crAzyli0n, WangWenjing, throwab ...

  6. iPhone 证书导出分享给多个开发人员操作

    1.应用程序->实用工具->keychain Access->操作如图1 2.将上图导出的证书.AppleWWDRCA.cer.mythlinkDeveloper.mobilepro ...

  7. 【hibernate】Hibernate中get()和load()的区别

    Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别. 1. get() 使用get()来根据ID进行单条查询: 1 User user= ...

  8. iOS -- SKPhysicsWorld类

    SKPhysicsWorld类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit. ...

  9. iOS开发 Coretext基本用法

    转至 http://blog.csdn.net/fengsh998/article/details/8691823 API接口文档. https://developer.apple.com/libra ...

  10. 手把手教你如何利用Meterpreter渗透Windows系统

    在这篇文章中,我们将跟大家介绍如何使用Meterpreter来收集目标Windows系统中的信息,获取用户凭证,创建我们自己的账号,启用远程桌面,进行屏幕截图,以及获取用户键盘记录等等. 相关Payl ...