HDU 2585 Hotel By Assassin 一道好的递归题

Problem Description
Last year summer Max traveled to California for his vacation. He had a great time there: took many photos, visited famous universities, enjoyed beautiful beaches and tasted various delicious foods. It is such a good trip that Max plans to travel there one more time this year. Max is satisfied with the accommodation of the hotel he booked last year but he lost the card of that hotel and can not remember quite clearly what its name is. So Max searched
in the web for the information of hotels in California ans got piles of choice. Could you help Max pick out those that might be the right hotel?

Input
Input may consist of several test data sets. For each data set, it can be format as below: For the first line, there is one string consisting of ‘‘,’?’and ‘a’-‘z’characters.This string represents the hotel name that Max can remember.The ‘‘and ‘?’is wildcard characters. ‘*’ matches zero or more lowercase character (s),and ‘?’matches only one lowercase character.

In the next line there is one integer n(1<=n<=300)representing the number of hotel Max found ,and then n lines follow.Each line contains one string of lowercase character(s),the name of the hotel.
The length of every string doesn’t exceed 50.

Output
For each test set. just simply one integer in a line telling the number of hotel in the list whose matches the one Max remembered.

Sample Input
herbert
2
amazon
herbert

?ert*
2
amazon
herbert

*
2
amazon
anything

herbert?
2
amazon
herber

Sample Output
1
0
2
0

思路:题目的意思是给你一个目标串,在给你k个串是否和目标串匹配,其中目标串中*代表0-n个字符,?代表任一个字符。

思路就是递归,如果遇到了*,找到每个要匹配大的串在该位置及其以后有没有尾部符合条件的,任意匹配即可

讲不清楚,一看代码就懂了~

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
bool find(string a,string b){
    int i,j;
    for(i=0;i<a.size();i++){
        if(a[i]=='*'){
            if(i==a.size()-1) return true;   //匹配到最后一定是对的 
            string c=a.substr(i+1);
            for(j=i;j<a.size();j++){         //从b串中找到一个符合条件的尾部 
                if(find(c,b.substr(j))){     //找到一个就够了返回true 
                    return true;
                }
            }
        }
        else{
            if(i>=b.size())return false;    //不允许出现b比a还小的情况 
            if(a[i]=='?')continue;           
            if(a[i]!=b[i]) return false;
        }
    }
    if(b.substr(a.size())!="")return false; //个人感觉应该加上,因为可能出现b比a长且前面正好匹配的情况 
    return true;
}

int main(){
    //input;
    int n;
    string a,b;
    while(cin>>a){
        scanf("%d",&n);
        int ans=0;
        while(n--){
            cin>>b;
            if(find(a,b)) ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值