HDU1075 What Are You Talking About

本文探讨了字典树和Map在数据处理中的应用,对比了两者在不同场景下的性能表现,字典树在特定查询上展现出更快的速度,而Map则在内存使用上更为宽松。文章通过一个具体的代码示例,展示了如何利用这两种数据结构进行字符串映射和查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

翻译  

字典树 和 map 都可以

 字典树 265 MS63592 KB  

map  1906MS 36100 KB

View Code
/*
scanf printf gets dont apply to string
can only use cin or cout
*/

#pragma warning( disable: 4786 )  //防止stl里面的warning
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>

using namespace std;   // cin / cout
using std::string;
using std::map;

int main()
{
    string str, str2;
    char c;
    map<string, string> mp;
//    str.resize(100);
    while(cin>>str)
    {
        if( str == "END" )
            break;
        if(str == "START")
            continue;
        cin >> str2 ;
        mp[str2] = str;
    }
    cin >> str; getchar();  // 缓冲中保留\n
    str = "" ;
    while(1)
    {
        while( c = getchar() )
        {
            if(c <= 'z' && c>= 'a')
                str += c;
            else break;
        }
        if( c == 'E' )
            break;
        if( mp.find(str) != mp.end() )
            cout<<mp[str];
        else
            cout<<str;
        printf("%c", c);
        str = "" ;
    }    
    cin >> str;  // 把最后ND输入完
    return 0;
}

 

转载于:https://www.cnblogs.com/crisxyj/archive/2013/03/12/2956041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值