关键字搜索

90worker 2011-08-31 04:35:59
关键字搜索
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 29, Accepted users: 17
Problem 10855 : No special judgement
Problem description
我们可爱的新网站具有了全新的搜索功能,使用了2个通配符“*”和“?”,其中“*”表示0或者多个小写字母,“?”代表1个字母。

当我们输入一个关键字的时候,我们在不确定的地方就使用通配符。我们在数据库里面有多条记录,每条记录都是由小写字母组成,现在给出一个关键字,你能告诉我数据库里面有多少条与关键字相匹配的记录吗?

例如: 如果关键字是j*y*m*y?,那么jiyanmoyu,jyanmoyu,jymyu都是相匹配的记录。



Input
多组测试数据,小于20组。

对于每组测试数据,第一行是输入的关键字,接下是数据库里面的所有记录的条数n,1<=n<=10000,每条记录的长度不超过50个小写字母。



Output
对于每组测试数据,输出与关键字想匹配的总记录条数,占一行。


Sample Input
jiyanmoyu
2
jiyanmoyu
huyanluanyu
ji*moy?
3
jiyanmoyu
jimoyu
huyanluanyu


Sample Output
1
2


Problem Source
HUNNU Contest


求大牛贴出代码 我是做了一天没做出来
...全文
148 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
十八道胡同 2011-09-01
  • 打赏
  • 举报
回复
http://blog.csdn.net/lcl_data/article/details/6739801
十八道胡同 2011-09-01
  • 打赏
  • 举报
回复
我的是IE9,csdn没有插件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

bool map(char *key, int keyI,char *tc, int tcI)
{
if(tc[tcI]=='\0')
{
if(key[keyI]=='\0' || key[keyI]=='*' )
{
return true;
}
return false;
}

switch (key[keyI])
{
case '?':
return map(key,keyI+1,tc,tcI+1); /*one key, one TC*/
case '*':
return map(key,keyI+1,tc,tcI) || map(key,keyI,tc,tcI+1)|| map(key,keyI+1,tc,tcI+1);/*0 OR 1+ OR 1 */
default:
return (key[keyI]==tc[tcI]) && map(key,keyI+1,tc,tcI+1);
}

}

int main()
{
/*input redirect to debug\\input.txt file*/
freopen("debug\\input.txt","r",stdin);

char key[55],tc[55];
int n=0,total=0;
while(scanf("%s",key),key[0]!='\0')
{
scanf("%d",&n);
total=0;
for (int i = 0; i < n; i++)
{
scanf("%s",tc);
if(map(key,0,tc,0)==true)
{
//printf("%s\n",tc);
total++;
}
}
printf("%d\n",total);
key[0]='\0';
}
return 0;
}
qq120848369 2011-08-31
  • 打赏
  • 举报
回复
就是个递归,真心没什么技术含量。
十八道胡同 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lcl_data 的回复:]

我记得boost里面有正则表达式的库函数,可否能用?
[/Quote]
ACM的话STL是可以用的
十八道胡同 2011-08-31
  • 打赏
  • 举报
回复
我记得boost里面有正则表达式的库函数,可否能用?
赵4老师 2011-08-31
  • 打赏
  • 举报
回复
//摘自《代码之美》
// 字符 含义
// . 匹配任意的单个字符
// ^ 匹配输入字符串的开头
// $ 匹配输入字符串的结尾
// * 匹配前一个字符的零个或者多个出现
#include <stdio.h>
int matchhere(char *regexp, char *text);

int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text
do {// a * matches zero or more instances
if (matchhere(regexp, text)) return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}
int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text
if (regexp[0] == '\0') return 1;
if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);
return 0;
}

int match(char *regexp, char *text) {// match: search for regexp anywhere in text
if (regexp[0] == '^') return matchhere(regexp+1, text);
do {// must look even if string is empty
if (matchhere(regexp, text)) return 1;
} while (*text++ != '\0');
return 0;
}
void main() {
printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));
printf("%d==match(^a ,abc)\n",match("^a" ,"abc"));
printf("%d==match(c$ ,abc)\n",match("c$" ,"abc"));
printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));
printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));
printf("-------------------\n");
printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));
printf("%d==match(^B ,abc)\n",match("^B" ,"abc"));
printf("%d==match(A$ ,abc)\n",match("A$" ,"abc"));
printf("%d==match(a..c,abc)\n",match("a..c","abc"));
printf("%d==match(a.*d,abc)\n",match("a.*d","abc"));
}
//1==match(abc ,abc)
//1==match(^a ,abc)
//1==match(c$ ,abc)
//1==match(a.c ,abc)
//1==match(a.*c,abc)
//-------------------
//0==match(ABC ,abc)
//0==match(^B ,abc)
//0==match(A$ ,abc)
//0==match(a..c,abc)
//0==match(a.*d,abc)
  • 打赏
  • 举报
回复
应该属于字符串匹配问题吧,没做过
一叶之舟 2011-08-31
  • 打赏
  • 举报
回复
没做过帮顶一下

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧