您的位置:首页 > 其它

Binary String Matching

2016-08-16 18:07 309 查看

问题 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[1111];
char str[1111];
char temp[1111];
int cou=0;
int b;
scanf("%d",&n);
for(int i=0;i<n;i++){
cou=0;
scanf("%s",pattern);
scanf("%s",str);
int lens=strlen(str);
int lenp=strlen(pattern);
for(int j=0;j<lens;j++){
b=0;
if(str[j]==pattern[0]){
for(int h=0;h<lenp;h++){
if(pattern[h]!=str[j+h]){
b=1;
break;
}else{
continue;
}
}
if(b==0){
cou++;
}
}
}
if(i==n-1){
printf("%d",cou);
}else{
printf("%d\n",cou);
}

}

return 0;
}
 只前在自己学校oj提交可以ac,但到别的oj提交就过不了,自己学校oj的测试数据太弱!!又重新写了一个
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>

using namespace std;

int main()
{
int n;
char ch;
int b=0;
char s[10005];
scanf("%d",&n);
for(int i=0;i<n;i++){
stack<char> st1;
scanf("%s",s);
int len=strlen(s);
st1.push(s[0]);
for(int j=1;j<len;j++){
b=0;
if(st1.empty()){
st1.push(s[j]);
}else{
if(s[j]==')'){
if(st1.top()=='('){
st1.pop();
}else{
printf("No\n");
b=1;
while(!st1.empty()){
st1.pop();
}
break;
}
}
if(s[j]==']'){
if(st1.top()=='['){
st1.pop();
}else{
printf("No\n");
b=1;
while(!st1.empty()){
st1.pop();
}
break;
}
}
if(s[j]=='['||s[j]=='('){
st1.push(s[j]);
}
}

}

if(b!=1&&st1.empty()){
printf("Yes\n");
}
if(b==0&&!st1.empty()){
printf("No\n");
}
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: