Binary String Matching

来源:互联网 发布:商城 域名取名 编辑:程序博客网 时间:2024/05/14 12:14
 
<dt style="font-family: Tahoma, Arial, sans-serif, simsun; line-height: 26px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">描述</dt><dd style="color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 14px; line-height: 26px; margin: 0px; padding: 0px;">Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit<div class="clr" style="clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输入</dt><dd style="margin: 0px; padding: 0px;">The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">输出</dt><dd style="margin: 0px; padding: 0px;">For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.</dd><dt style="margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-size: 16px; font-weight: bold;">样例输入</dt><dd style="margin: 0px; padding: 0px;"><pre id="sample_input" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em; background-color: rgb(239, 239, 239);">31110011101101011100100100100011010110100010101011 
样例输出
303 
分析

#include<iostream>#include<string>using namespace std;int main(){string s1,s2;int n;cin>>n;while(n--){cin>>s1>>s2;unsigned int m=s2.find(s1,0);int num=0;while(m!=string::npos){num++;m=s2.find(s1,m+1);}cout<<num<<endl;}}        

0 0
原创粉丝点击