binary string match

我是逐个比较,每次取出对应的位置来比较。。当然也可以用strcmp,只是字符数组取出来还要变成字符串加\0比较烦。。

这个算法一定要注意flag的重置位置。。第一次就搞错了

Mark April 7,Monday 2014 USC

有问题大家留言~?

重新换了一下c++中得string find写了一下。。简洁了很多。

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;


int main()
{
char strA[11];
char strB[1001];
bool flag=true;
int line,lenA,lenB,match=0;
cin>>line;
while(line--)
{
    match=0;
	cin>>strA;
	cin>>strB;
	lenA=strlen(strA);
	lenB=strlen(strB);
   for(int i=0;i<(lenB-lenA+1);i++)
   {   flag=true;
	   for(int j=0;j<lenA;j++)
	   {
		   if(strA[j]!=strB[j+i])
		   {
			   flag=false;
			   break;
		   }
	   }
	   if(flag==true)
		   match++;

   }
   cout<<match<<endl;

}


}

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
using namespace std;

int main()
{
string s1,s2;
int n;
cin>>n;
while(n--)
{
	cin>>s1>>s2;
	int m=-1;
	int num=-1;
    do
    {   num++;
    	m=s2.find(s1,m+1);

    }while(m!=string::npos);
	cout<<num<<endl;

}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值