Binary String Matching

问题 B: Binary String Matching

时间限制: 3 Sec  内存限制: 128 MB
提交: 4  解决: 2
[提交][状态][讨论版]

题目描述

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

输入

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.

输出

For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.

样例输入

3
11
1001110110
101
110010010010001
1010
110100010101011

样例输出

3
0
3
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main()
{
int n;
char pattern[];
char str[];
char temp[];
int cou=;
int b;
scanf("%d",&n);
for(int i=;i<n;i++){
cou=;
scanf("%s",pattern);
scanf("%s",str);
int lens=strlen(str);
int lenp=strlen(pattern);
for(int j=;j<lens;j++){
b=;
if(str[j]==pattern[]){
for(int h=;h<lenp;h++){
if(pattern[h]!=str[j+h]){
b=;
break;
}else{
continue;
}
}
if(b==){
cou++;
}
}
}
if(i==n-){
printf("%d",cou);
}else{
printf("%d\n",cou);
} } return ;
}

 

只前在自己学校oj提交可以ac,但到别的oj提交就过不了,自己学校oj的测试数据太弱!!

又重新写了一个

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring> using namespace std; int main()
{
int n;
char ch;
int b=;
char s[];
scanf("%d",&n);
for(int i=;i<n;i++){
stack<char> st1;
scanf("%s",s);
int len=strlen(s);
st1.push(s[]);
for(int j=;j<len;j++){
b=;
if(st1.empty()){
st1.push(s[j]);
}else{
if(s[j]==')'){
if(st1.top()=='('){
st1.pop();
}else{
printf("No\n");
b=;
while(!st1.empty()){
st1.pop();
}
break;
}
}
if(s[j]==']'){
if(st1.top()=='['){
st1.pop();
}else{
printf("No\n");
b=;
while(!st1.empty()){
st1.pop();
}
break;
}
}
if(s[j]=='['||s[j]=='('){
st1.push(s[j]);
}
} } if(b!=&&st1.empty()){
printf("Yes\n");
}
if(b==&&!st1.empty()){
printf("No\n");
}
}
return ;
}
上一篇:Windows系统bug


下一篇:SPI通信