九度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 题目描述: 输入 ...
随机推荐
- net4:Panel动态添加控件及隐藏,Table动态创建表格
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- pexpect模块
pexpect用来启动子程序,使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的python模块,当然我们可以使用他来做ssh登陆,ssh模块登陆还有一个基于python实现远程连接,用于s ...
- py2exe打包整个项目
这段时间做了用Python做了一个科学计算的项目,项目中用到了很多的第三方Python库,包括PyQt.traits.traitsui.matplotlib.pyface.table.numpy.tv ...
- Delphi 之Copyrect的使用
http://cqujsjcyj.iteye.com/blog/380970 Copyrect的使用(图片复制.放大.以及做图片放大镜等)一.从一个选取一个区域中的图象到另一个图象组件中的固定区域pr ...
- HDU 4691
http://acm.hdu.edu.cn/showproblem.php?pid=4691 留个板子. #include <iostream> #include <cstdio&g ...
- springboot使用Mybatis(xml和注解)全解析
刚毕业的第一份工作是 java 开发,项目中需要用到 mybatis,特此记录学习过程,这只是一个简单 demo,mybatis 用法很多不可能全部写出来,有更复杂的需求建议查看 mybatis ...
- Java中获取ServletContext的方法
Servlet: this.getServletContext() this.getServletConfig().getServletContext() request.getSession().g ...
- Android 测试自定义纯数字软键盘
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- 【js】前台调试,在浏览器调试环境下找不到js怎么办?
针对这次 整个项目单页面的情况下,所有点击出现的新页面都是追加在母页面的情况下,很多时候不像原本的情况,可以直接在浏览器的调试环境下找到想要调试的js代码 这种情况下,怎么能找到子页面的js代码,调试 ...
- DICOM医学图像处理:Orthanc Plugin SDK实现WADO服务
背景: Orthanc是博主发现的一个很完美的DICOM和HTTP服务端开源软件,前几篇分别介绍了Orthanc的基本使用.Orthanc从0.8.0版本之后给出了Plugin SDK,通过该SDK可 ...