zoukankan      html  css  js  c++  java
  • HDU 2585 Hotel(字符串的模糊匹配+递归)

    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
     
    字符串的模糊匹配,?可以当做都匹配的一个字符,遇到*时自带一个查找
     
     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<string>
     6 
     7 using namespace std;
     8 
     9 char a[55],b[55];
    10 int n,len1,len2,res;
    11 
    12 bool digital(int i,int j)
    13 {
    14     //结尾判定
    15     if(i==len1&&j==len2)
    16         return true;
    17     else if(i==len1||j==len2)
    18         return false;
    19     //中间过程
    20     else if(a[i]=='*')
    21     {
    22         for(int k=j;k<=len2;k++)
    23             if(digital(i+1,k))
    24             return true;
    25     }
    26     else if(a[i]=='?'||a[i]==b[j])
    27         digital(i+1,j+1);
    28     else
    29         return false;
    30 }
    31 
    32 int main()
    33 {
    34     while(scanf("%s",&a)!=EOF)
    35     {
    36         scanf("%d
    ",&n);
    37         len1=strlen(a);
    38         res=0;
    39         while(n--)
    40         {
    41             scanf("%s",&b);
    42             len2=strlen(b);
    43             if(digital(0,0))
    44                 res++;
    45         }
    46         printf("%d
    ",res);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    Bzoj 4408: [Fjoi 2016]神秘数 可持久化线段树,神题
    Bzoj 4034: [HAOI2015]T2 树链剖分,子树问题,dfs序
    Bzoj 1984: 月下“毛景树” 树链剖分
    面试题14:调整数组顺序使奇数位于偶数前面
    面试题13:在O(1)时间删除链表结点
    面试题12:打印1到最大的n位数
    面试题11:数值的整数次方
    面试题10:二进制中1的个数
    [找程序员代写推荐]不要说你工作多久,多厉害!这些题不从网上找答案,你能做出来吗???
    [原]Android开发技巧--ListView
  • 原文地址:https://www.cnblogs.com/Annetree/p/7144816.html
Copyright © 2011-2022 走看看