HDU 2585 Hotel
2015-02-25 21:42
239 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
herbert
2
amazon
herbert
?ert*
2
amazon
herbert
*
2
amazon
anything
herbert?
2
amazon
herber
[align=left]Sample Output[/align]
1
0
2
0
[align=left]Source[/align]
ECJTU 2009 Spring Contest
Solution
给出一个字符串,问下列字符串中与所给匹配的有几个,?匹配一个字母,*匹配0或多个字母。
一开始想使用暴力扫一遍就好啦,结果*号有些难处理。
模糊匹配的话是可以用简单dp来做的,f[i][j]代表前i个字符和前j个字符匹配,注意*的话是和后面的所有字符都匹配的
dp的key
f[0][0] = true;//初始化
for (i = 1; i <= ls; ++i)
for (j = 1; j <= lx; ++j)
{
if ((s[i-1] == x[j-1] || s[i-1] == '?') && f[i-1][j-1]) f[i][j] = true;
if (s[i-1] == '*' && f[i-1][j-1])
{
for (k = j-1; k <= lx; ++k) f[i][k] = true;//可以匹配后面的所有,注意匹配0个的时候是j-1
break;
}
}后来参考他人的思路,可以用递归,这样比较好理解。
#include <cstdio>
#include <cstring>
char s[55], t[55];
int ls, lt;
bool check(int lens, int lent)
{
if (lens == ls && lent == lt) return true;
if (lens == ls || lent == lt) return false;
if (s[lens] == '?' || s[lens] == t[lent]) return check(lens+1, lent+1);
if (s[lens] == '*') for (int i = lent; i <= lt; ++i) if (check(lens+1, i)) return true;//往后看看是不是可以匹配
return false;
}
int main()
{
while (scanf("%s", s) != EOF)
{
ls = strlen(s);
int n, c = 0;
scanf("%d", &n);
while (n--)
{
scanf("%s", t);
lt = strlen(t);
if (check(0, 0)) ++c;
}
printf("%d\n", c);
}
return 0;
}
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
herbert
2
amazon
herbert
?ert*
2
amazon
herbert
*
2
amazon
anything
herbert?
2
amazon
herber
[align=left]Sample Output[/align]
1
0
2
0
[align=left]Source[/align]
ECJTU 2009 Spring Contest
Solution
给出一个字符串,问下列字符串中与所给匹配的有几个,?匹配一个字母,*匹配0或多个字母。
一开始想使用暴力扫一遍就好啦,结果*号有些难处理。
模糊匹配的话是可以用简单dp来做的,f[i][j]代表前i个字符和前j个字符匹配,注意*的话是和后面的所有字符都匹配的
dp的key
f[0][0] = true;//初始化
for (i = 1; i <= ls; ++i)
for (j = 1; j <= lx; ++j)
{
if ((s[i-1] == x[j-1] || s[i-1] == '?') && f[i-1][j-1]) f[i][j] = true;
if (s[i-1] == '*' && f[i-1][j-1])
{
for (k = j-1; k <= lx; ++k) f[i][k] = true;//可以匹配后面的所有,注意匹配0个的时候是j-1
break;
}
}后来参考他人的思路,可以用递归,这样比较好理解。
#include <cstdio>
#include <cstring>
char s[55], t[55];
int ls, lt;
bool check(int lens, int lent)
{
if (lens == ls && lent == lt) return true;
if (lens == ls || lent == lt) return false;
if (s[lens] == '?' || s[lens] == t[lent]) return check(lens+1, lent+1);
if (s[lens] == '*') for (int i = lent; i <= lt; ++i) if (check(lens+1, i)) return true;//往后看看是不是可以匹配
return false;
}
int main()
{
while (scanf("%s", s) != EOF)
{
ls = strlen(s);
int n, c = 0;
scanf("%d", &n);
while (n--)
{
scanf("%s", t);
lt = strlen(t);
if (check(0, 0)) ++c;
}
printf("%d\n", c);
}
return 0;
}
相关文章推荐
- Hdu_2585 Hotel(递归)
- HDU 2585 Hotel(字符串的模糊匹配+递归)
- hdu 2585 Hotel 很好的递归模拟题
- HDU-3667 Hotel 线段树 经典空间合并
- HDU 3193 Find the hotel
- HDU Find the hotel
- HDU 3308 LCSI 【线段树】 与 poj 3667 hotel
- HDU 2871 Memory Control (恶心线段树,HOTEL加强版)
- Poj 3667 Hotel + Hdu 4553 约会安排 (线段树最左空区间)
- HDU ACM 16 2585 hotel
- 【HDOJ】2585 Hotel
- POJ 3667 Hotel & HDU 2871 Memory Control 线段树区间合并
- HDU-3193 Find the hotel
- Hdu 3139 Find the hotel
- HDU 2022 海选女主角
- hdu 1342 Lotto (简单dfs)
- HDU-1247-Hats Words
- HDU-4283 You Are the One 区间DP
- HDU 1423 最长上升公共子序列
- hdu 3452(最小割)