memcmp函数——比较 str1 和 str2的前n个字符

1、函数:int memcmp(const void *str1, contst void *str2, size_t n)

2、用法:memcmp(str1,str2,n)

                比较 str1 和 str2的前n个字符,相等则返回0

3、头文件 :#include < string.h >


例题:

剪花布条

  HDU - 2087 
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? 
Input输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。 
Output输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。 
Sample Input
abcde a3
aaaaaa  aa
#
Sample Output
0
3

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;

int main()
{
	char s1[1005],s2[1005];
	while(scanf("%s",s1)!=EOF)
	{
		if(s1[0]=='#')
			break;
		scanf("%s",s2);
		int ans=0;
		int len1=strlen(s1),len2=strlen(s2);
		for(int i=0;i<=len1-len2;i++)
		{
			if(memcmp(s2,s1+i,len2)==0)
			{
				ans++;
				i+=len2-1; 
			} 
			
		}
		printf("%d\n",ans);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/lvjinyanjy/article/details/80080253
今日推荐