Babelfish--POJ 2503

本文介绍了一种使用map和trie树进行字符串匹配的方法。通过构建字典或trie树,可以高效地判断输入字符串是否存在于预设的集合中。文章提供了一个具体的C++实现案例,并讨论了处理空白字符等细节。

1、题目类型:字符串、map、trie树。

2、解题思路:(1)根据输入建立map字典或者trie树;(2)输入str即查找字典中是否存在该str。

3、注意事项:注意对于单行空白的处理。

4、实现方法:

#pragma warning (disable:4786)
#include
<iostream>
#include
<map>
#include
<string>
using namespace std;

int main()
{
map
<string,string> M;
char ch[1000];
string str1,str2;
while(1)
{
cin.getline(ch,
sizeof(ch));
if(strlen(ch)==0)
break;
int len=strlen(ch);
str1
=str2="";
int flag=1;
for(int i=0;i<len;i++)
{
if(flag && ch[i]!=' ')
{
str1
+=ch[i];
}
else if(ch[i]==' ')
flag
=0;
else
str2
+=ch[i];
}
M[str2]
=str1;
}
while(cin>>str1)
{
if(M[str1]!="")
cout
<<M[str1]<<endl;
else
cout
<<"eh"<<endl;
}
return 0;
}

 

转载于:https://www.cnblogs.com/yongze103/archive/2010/08/20/1804928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值