akoj-1232-找模式串
来源:互联网 发布:数据库中create table 编辑:程序博客网 时间:2024/06/07 07:41
找模式串
Time Limit:1000MS Memory Limit:65536K
Total Submit:14 Accepted:10
Description
在字符串中查找指定的模式串是一种常见的运算,称为模式匹配。请你编写实现模式匹配的程序。
Input
输入数据的第一行是一个正整数T(0<T≤100),表示有T组测试数据。
每组测试数据有两行:第一行为字符串S(长度不超过128,全部为大写英文字母),第二行为模式串P(长度不超过20)。
Output
对于每组测试数据,在一行上输出一个整数,表示模式串P在字符串S中的位置序号(若出现多次,则输出第一次出现时的位置)。若在字符串S中找不到模式串P,则输出整数-1。
Sample Input
2ABCDEFABEJBBMSWJPREAEYBMMBWEJ
Sample Output
0-1
Source
[Submit] [Go Back] [Status] [Discuss]
#include<iostream>#include<cstring>#include<string>using namespace std;int Brute_Force(string s,string t) //简单匹配算法{ int i=0,j=0; while(i<s.length()&&j<t.length()) { if(s[i]==t[j]) {i++;j++;} else {i=i-j+1;j=0;} } if(j>=t.length()) return (i-t.length()); else return -1;}int main(){ string s,t; int n; cin>>n; while(n--) { cin>>s>>t; cout<<Brute_Force(s,t)<<endl; } return 0;}
0 0
- akoj-1232-找模式串
- 1232 -- 找模式串
- akoj-1079-周期串
- Problem 1032 - 找模式串
- 代理模式---有事找助理
- akoj-1207-亲和数
- akoj-1018-最长平台
- akoj-1053-大数阶乘
- akoj-1211-统计数字
- akoj-1219-扑克牌
- akoj-1187-统计数字
- akoj-1138-游戏编码
- akoj-1142-同构词
- akoj-1264-足球世界杯小组赛
- akoj-1012-机器指令
- akoj-1271-Fibonacci numbers
- akoj-1272-字母统计
- akoj-1082-螺旋矩阵
- mongodb shell问题
- python 的日志logging模块学习
- USACO 1.3.3 Prime Cryptarithm
- [水题][第一阶段-回归水题][HDOJ-2028]Lowest Common Multiple Plus
- akoj-1244-括号的深度
- akoj-1232-找模式串
- MacBook Pro 安装myeclipse 关于虚拟内存的问题
- Linux守护进程
- Ajax动态刷新元素js事件失效
- 高效的把NSMutableArray变成NSArray
- [水题][第一阶段-回归水题][HDOJ-2029]Palindromes _easy version
- ExpandableListView / ExpandableListActivity 使用及数据更新
- [Cordova学习]4.Cordova(iOS)架构和实现
- 再看出参---分析GetMemory的错误