HDU 2087 剪花布条(STL、暴力)

Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
 


 

Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
 


 

Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
 


 

Sample Input
abcde a3 aaaaaa aa #
 


 

Sample Output
0 3


先上STL做法。

string.find(str2) :当str2是str的子串时,返回其在str中第一次出现的位置;如果str2不是str的子串,返回string::npos。

string::npos是一个unsigned_int类型的常数,作为string::find()匹配失败的返回值。

#include 
#include 
#include 
#include 
using namespace std;
int main(){
    string a,b;
    int ans;
    while(cin>>a){
        ans=0;
        if(a=="#")
            break;
        cin>>b;
        while(a.find(b)!=string::npos){
            a.erase(a.find(b),b.length());
            ans++;
        }
        cout<

暴力做法:

拿k记录下当前a遍历的位置进而下次从该位置开始遍历。注意for循环的处理。

#include 
#include 
#include 
#include 
using namespace std;
int main(){
    string a;
    string b;
    int k = 0;
    int flag;
    int ans=0;
    while(true){
        a="";b="";
        cin>>a;
        if(a=="#")
            break;
        cin>>b;
        for(int i=0;i

你可能感兴趣的:(蓝桥杯,ACM)