POJ_2503_Babelfish(Trie/map)

描述



http://poj.org/problem?id=2503

给出一个字典,求翻译,翻译不了输出eh.

分析



网上看到map可直接做,但我就是想打打Trie模板...

p.s.据说哈希也能做,但我完全不知道那是啥...

Trie做法:在每个单词节点存下对应翻译的字符串.

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int type=26;
struct Trie{
    struct node{
        node* next[type];
        bool v;
        char word[15];
        node(){
            v=false;
            for(int i=0;i<type;i++) next[i]=NULL;
            for(int i=0;i<15;i++) word[i]=‘\0‘;
        }
    }*root;
    Trie(){ root=new node; }
    void insert(char *c1,char *c2){
        node *o=root;
        while(*c2){
            int t=*c2-‘a‘;
            if(o->next[t]==NULL) o->next[t]=new node;
            o=o->next[t];
            c2++;
        }
        o->v=true;
        strcpy(o->word,c1);
    }
    void query(char *c){
        node* o=root;
        while(*c){
            int t=*c-‘a‘;
            if(o->next[t]==NULL){
                printf("eh\n");
                return;
            }
            o=o->next[t];
            c++;
        }
        if(o->v) printf("%s\n",o->word);
        else printf("eh\n");
    }
}tree;

int main(){
    char c[25],a[15],b[15];
    while(cin.getline(c,25)){
        if(c[0]==‘\0‘) break;
        sscanf(c,"%s %s",a,b);
        tree.insert(a,b);
    }
    while(cin.getline(c,25)){
        if(c[0]==‘\0‘) break;
        tree.query(c);
    }
    return 0;
}

Trie

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;

char c[25],a[15],b[15];
map <string,string> m;

int main(){
    while(cin.getline(c,25)){
        if(c[0]==‘\0‘) break;
        sscanf(c,"%s %s",a,b);
        m[b]=a;
    }
    map <string,string> :: iterator it;
    while(cin.getline(c,25)){
        if(c[0]==‘\0‘) break;
        it=m.find(c);
        if(it!=m.end()){
            printf("%s\n",it->second.c_str());
        }
        else{
            printf("eh\n");
        }
    }
    return 0;
}

map

时间: 2024-08-28 22:25:20

POJ_2503_Babelfish(Trie/map)的相关文章

POJ_2503_Babelfish(map or 字典树)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 34816   Accepted: 14908 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

布隆过滤器(Bloom Filter)的原理和实现

什么情况下需要布隆过滤器? 先来看几个比较常见的例子 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 yahoo, gmail等邮箱垃圾邮件过滤功能 这几个例子有一个共同的特点: 如何判断一个元素是否存在一个集合中? 常规思路 数组 链表 树.平衡二叉树.Trie Map (红黑树) 哈希表 虽然上面描述的这几种数据结构配合常见的排序.二分搜索可以快速高效的处理绝大部分判断元素是否存在集合中的需求.但是当集合里

布隆过滤器认知

布隆过滤器 (Bloom Filter)是由Burton Howard Bloom于1970年提出,它是一种space efficient的概率型数据结构,用于判断一个元素是否在集合中. 看看下面几个问题: 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 邮箱垃圾邮件过滤功能 以上这些场景有个共同的问题:如何查看一个东西是否在有大量数据的池子里面. 通常做法有以下几种思路: 数组 链表 树.平衡二叉树.Trie

A1-2017级算法上机第一次练习赛 I jhljx学位运算

Problem Description jhljx在C++程序设计课程中学习了位运算这个高端的知识,现在他开始上算法课,决定活学活用.. 位运算中异或是一个比较神奇的操作,有0^1=1,1^0=1,0^0=0,1^1=0. 于是jhljx给你了一个长度为n的数组,让你求出该数组的某个子数组(数组中下标连续的一些元素组成的数组)的异或值之和. Input 输入多组数据.对于每组数据,第一个行为数据组数n(1<=n<=1000000)第二行为n个数组元素(保证元素在int范围内)第三行为一个正整数

区块链中的密码学之默克尔树(十五)

目录 1. 前言 2. 默克尔树 3. 布隆过滤器 什么情况下需要布隆过滤器? 常规思路 布隆过滤器介绍 布隆过滤器原理 布隆过滤器添加元素 布隆过滤器查询元素 4. 同态加密 4.1 概览:同态加密的概念 4.2 同态加密的定义.安全性和简单实例 5. 零知识证明 零知识证明的提出 零知识证明的形式化定义 零知识证明满足的性质 基本的零知识协议 非交互式零知识证明 零知识证明的应用 1. 前言 2. 默克尔树 默克尔树( 又叫哈希树) 是一种二叉树,由一个根节点.一组中间节点和一组叶节点组成.

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

[ACM] POJ 2418 Hardwood Species (Trie树或map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

[ACM] POJ 2418 Hardwood Species (Trie树或者map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

Sevenk Love Oimaster(trie,MAP后缀自动机)

题意:给出n个串,再m个询问,每次询问一个串s,问给出的n个串中,子串包含s的有几个 解法:给这n个串建立trie,再将trie建成sam,然后我们要知道的是,对于每一个状态u所表示的子串,被几个串包含,这里跟http://blog.csdn.net/no__stop/article/details/38611209这题的处理方法类似,不再赘述.然后询问的串,就去sam上匹配,匹配到哪个节点,就将该节点被几个串包含输出即可.这题字符集有128,建立sam的时候,如果把无用的边也枚举一遍,复杂度过