一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
abcde a3 aaaaaa aa #
03
-----------------------------------我是倒霉的分割线------------------------------------------------------------
思路分析:
进行字符串匹配。
回顾总结:
要熟练掌握字符串操作的一些函数,和string与char*的一些特性,不要把两者搞混了。
掌握find()函数的使用,第一个参数是匹配串,第二个参数是匹配的位置。如果没有匹配到,会返回string::npos
#include <iostream> #include<string> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char** argv) { int n; string a,b; while(1) { cin>>a; if(a[0]=='#') break; cin>>b; //int loc=s.find_first_of(" "); // string a=s.substr(0,loc-1); //string b=s.substr(loc+1,s.length()-1); int count=0,begin; int size=b.length(); begin=0-size; while((begin=a.find(b,begin+size))!=string::npos) { count++; } printf("%d\n",count); } return 0; }