九度OJ 1094:String Matching(字符串匹配) (计数)
时间限制: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
 
思路:
简单的计数题。
代码:
#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(字符串匹配) (计数)的更多相关文章
- 九度oj题目1165:字符串匹配
		
题目1165:字符串匹配 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3078 解决:1079 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中 ...
 - 九度OJ 1021:统计字符 (基础题)
		
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5418 解决:3146 题目描述: 统计一个给定字符串中指定的字符出现的次数. 输入: 测试输入包含若干测试用例,每个测试用 ...
 - 九度OJ 1199:找位置 (计数)
		
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2083 解决:1010 题目描述: 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1:a,4 ...
 - 九度OJ 1182:统计单词 (计数)
		
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3920 解决:1443 题目描述: 编一个程序,读入用户输入的,以"."结尾的一行文字,统计一共有多少个单词,并分别输出 ...
 - 九度OJ 1149:子串计算 (计数、排序)
		
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1026 解决:571 题目描述: 给出一个01字符串(长度不超过100),求其每一个子串出现的次数. 输入: 输入包含多行,每行一个字符串. ...
 - 九度OJ 1098:字母统计 (计数)
		
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3720 解决:1809 题目描述: 输入一行字符串,计算其中A-Z大写字母出现的次数 输入: 案例可能有多组,每个案例输入为一行字符串. 输 ...
 - 九度oj题目&吉大考研11年机试题全解
		
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
 - 九度OJ题目1105:字符串的反码
		
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
 - 【九度OJ】题目1054:字符串内排序 解题报告
		
[九度OJ]题目1054:字符串内排序 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1054 题目描述: 输入 ...
 
随机推荐
- declaration specifier, declarator, type specifier
			
static struct abc * b; static struct abc : declaration specifier * b : declarator struct abc : type ...
 - QQ客服代码,支持临时会话
			
<a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=QQ号&site=qq&m ...
 - hdu 1181(Floyed)
			
变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submis ...
 - java parse 带英文单词的日期字符串(转化新浪微博api返回的时间)
			
String str = "Sun Sep 23 00:32:57 +0800 2012"; SimpleDateFormat dateFormat = new SimpleDat ...
 - SnakeYaml使用
			
新的项目中需要将yaml文件解析为对象,调研了决定使用snakeYaml,下面看一看怎么使用. 一.引入依赖 因为项目是使用maven构建的,所以我们在pom文件中引入snakeYaml的依赖,如下: ...
 - UIAlertView弹出视图动画效果
			
在App设计中为了加强用户体验,我们会常常加入一些友好的动画效果.比如类似UIAlertView弹出的动画效果,由于系统中并没有直接提供类似的动画API,如果我们想要做出一样的效果,那就得深入的研究一 ...
 - 邁向IT專家成功之路的三十則鐵律 鐵律二十三:IT人的成家之道-樸實
			
根據內政部一份2013年最新的調查報告指出台灣人的離婚率位居全球第三,想想看如果這是經濟成長率的排名表現那該有多好.然而究竟為何在台灣這塊小小的土地上,不僅離婚非常高而且晚婚的人也非常的多,其原因肯定 ...
 - Java并发学习 & Executor学习 & 异常逃逸 & 同步互斥Best Practice & wait/notify, conditon#await/signal
			
看了这篇文章:http://www.ciaoshen.com/2016/10/28/tij4-21/ 有一些Java并发的内容,另外查了一些资料. 朴素的Thread 首先,Java中关于线程Thre ...
 - ActiveX控件打包成Cab置于网页中自动下载安装 [转]
			
http://blog.sina.com.cn/s/blog_520c32270100nopj.html 做过ActiveX控件的朋友都知道,要想把自己做的ActiveX控件功能放在自己的网页上使用, ...
 - vim 树形菜单插件NERDTree 的安装
			
vim 树形菜单插件的安装 NERDTree 1. mkdir ~/.vim cd ~/.vim mkdir bundle mkdir autoload 2. curl -Sso ~/.vim/au ...