九度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 题目描述: 输入 ...
随机推荐
- 【CF679B】Theseus and labyrinth(数学,贪心)
题意: 给一个m<=10^15,每次都减最接近当前值的立方数 让你找一个不大于m的最大的数并且这个数是减法次数最多的数 思路:见http://blog.csdn.net/miracle_ma/a ...
- 一简单c++程序之反汇编
#include<iostream> using namespace std; class point3d; class point2d; class point3d { private: ...
- py2exe打包整个项目
这段时间做了用Python做了一个科学计算的项目,项目中用到了很多的第三方Python库,包括PyQt.traits.traitsui.matplotlib.pyface.table.numpy.tv ...
- 总结下常用js中的小语法和技巧
1,数组对象遍历 对一个级数对象进行遍历,取出每个值 var arr={ "result":[ {"time":"2018-10-24 12:12:1 ...
- HDU 5266 pog loves szh III(区间LCA)
题目链接 pog loves szh III 题意就是 求一个区间所有点的$LCA$. 我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c ...
- SSH命令总结
目录 一.ssh命令 二.端口转发 三.scp 命令 四.rsync命令 五.sz和rz命令 六. ssh-agent 七.ssh执行命令不退出问题 参考文章 一.ssh命令 登录类型 密码登录: 服 ...
- CDOJ_327 BerOS file system
原题地址:http://acm.uestc.edu.cn/#/problem/show/327 The new operating system BerOS has a nice feature. I ...
- python pip包管理器安装
下载 http://peak.telecommunity.com/dist/ez_setup.py 执行:python ez_setup.py 下载: http://pypi.python.or ...
- 简约至上.md
中秋花了一天多时间阅读了简约至上这本书,书中内容不多,主要是向我们传达了产品设计的4个要素,给了产品经理设计产品时的一些要义指导; 一产品定位 在进行产品设计之前,首页需要对这款产品的商业定位需要有个 ...
- CSS 发明者 Håkon Wium Lie 访谈--csdn zhangxin09
原文地址:https://dev.opera.com/articles/css-twenty-years-hakon/ ---------------------------------------- ...