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

启发博客:http://blog.csdn.net/light_14/article/details/43940765

字符串的模糊匹配,?可以当做都匹配的一个字符,遇到*时自带一个查找

 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",&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\n",res);
47     }
48     return 0;
49 }
时间: 2024-08-11 18:44:50

HDU 2585 Hotel(字符串的模糊匹配+递归)的相关文章

[转载]java字符串模糊匹配(

,需要在java中进行字符串的模糊匹配,由于要比较的对象长度不同,也没有固定的组成规则,收集到下列三个方法解决问题 方法一. public int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引.返回的整数是 this.startsWith(str, k)为 true 的最小 k 值. 参数:str - 任意字符串. 返回:如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引:如果它不作为一个子字符串出现,则返回 -1.

字符串中的匹配之递归

字符串的括号匹配是一个很常见的问题.用栈这种后进先出的结构是非常适合的.此外,字符串中的回文以及衍生的各种问题也是字符串处理中非常常见的. 今天再说一下这类相似的问题,如何用递归来转化成子结构来求解. 先放一条LeetCode例题: 680. Valid Palindrome II Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome

HDU 1274 展开字符串 (递归+string类)

题目链接:HDU 1274 展开字符串 中文题. 左括号进入DFS函数,右括号return到上一层. 注意return回去的是这个一层递归中的括号中的字母串. AC代码: #include<stdio.h> #include<iostream> #include<string.h> #include<string> using namespace std; char str[300]; bool vis[300]; int len; string dfs(i

C++实现的字符串模糊匹配

C++基本没有正则表达式功能,当然像Boost里提供了正则.本文来源于博客园园友的一篇文章,请看: C/C++ 字符串模糊匹配 很早之前就看过这篇文章,原作者的需求很明确.代码实现也很好. 之所以又写这篇文章,是因为原作者只介绍了在Linux系统下直接调用系统函数fnmatch即可实现,而没有考虑在Windows在的使用. 本人这周看了下Google-glog代码,恰巧发现了一个类似fnmatch的简单实现,因此综合起来提供了一个跨平台的接口. 直接拿原作者的需求为例(再次感谢原作者大熊先生,我

dev 中 字符串转中文拼音缩写,对grid列表进行模糊匹配,grid获取焦点行,gridlookupedit控件用拼音模糊匹配下拉选项

番外篇:. //该方法是将字符串转化为中文拼音的首写字母大写, public static string RemoveSpecialCharacters(string str){try{if (string.IsNullOrWhiteSpace(str)) { return str; }var result = Regex.Replace(str, "[^0-9A-Za-z]", "");if (string.IsNullOrWhiteSpace(result))

c2java 动态规划之模糊匹配

字符串匹配 精确: indexOf(String str); -- strstr(), O(mn). lastIndexOf(String str); -- continue 的别样用法. matches(String regex); -- Regex.compile()/match(). 模糊: java package? Spell Checker -- 两个字符串的相似程度 Fuzzy Finder -- 子列匹配 上面两个问题都可以用这个概念"编辑距离"来有效解决. 所谓这个距

hdu 5510 Bazinga(字符串kmp)

Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2287    Accepted Submission(s): 713 Problem Description Ladies and gentlemen, please sit up straight.Don't tilt your head. I'm serious.For

selenium模糊匹配控件

起因:在查找一些控件时,可能控件的一些属性是变化的,那在匹配时需要进行模糊匹配,模糊匹配,使用xpath 定位方式有种: contains(属性名,字符串):使用文本匹配,功能很强大 starts-with(属性名,字符串):根据开头进行模糊匹配 ends-with(属性名,字符串):根据结尾内容进行匹配 matchs(属性名,字符串):根据正则进行匹配 案例: 如图,点击底部的一个收藏,弹出OK按钮,需要点击这个Ok,就能正常执行下一步 <span type="1">OK

selenium2 python 学习笔记--xpath模糊匹配

xpath模糊匹配,类似find_by_partial_link,如下图: contains(属性名,字符串),starts-with(属性名,字符串),ends-with(属性名,字符串),matchs(属性名,字符串)