我是逐个比较,每次取出对应的位置来比较。。当然也可以用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;
}
}