1. 程式人生 > >POJ 2503 Babelfish(STL、二分、字典樹、雜湊)

POJ 2503 Babelfish(STL、二分、字典樹、雜湊)

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 a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”.

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

題目大意

在給出字典資料時,由後邊的數查詢前面的數,然後給出檢索條目,輸出查詢結果,若字典中不存在,輸出“eh”。

實現方法

1、STL

C++能過,G++TLE

#include <iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
map<string,bool>exi;
map<string,string>mp;
int main()
{
    char str1[11],str2[11];
    char t;
    while(true)
    {
        if((t=getchar())=='\n'
) break; str1[0]=t; int i=1; while(true) { t=getchar(); if(t==' ') { str1[i]='\0'; break; } else str1[i++]=t; } cin>>str2; getchar(); mp.insert(make_pair<string,string>(str2,str1)); exi.insert(make_pair<string,bool>(str2,true)); } while(cin>>str1) { if(exi[str1]) cout<<mp[str1]<<endl; else cout<<"eh"<<endl; } return 0; }

2、二分

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100007
#define maxl 17
struct node
{
    char str1[maxl];
    char str2[maxl];
}s[maxn];
int num=0;
bool cmp(node x,node y)
{
    return strcmp(x.str2, y.str2) < 0;
}
int binary_find(char *str)
{
    int l=0,r=num-1;
    while(l<=r)
    {
        int m=(l+r)/2;
        if(strcmp(s[m].str2,str)==0)
            return m;
        if(strcmp(s[m].str2,str)<0)
            l=m+1;
        else if(strcmp(s[m].str2,str)>0)
            r=m-1;
    }
    return -1;
}

int main()
{
    char str[maxl*2];
    while(true)
    {
        gets(str);
        if(str[0] == '\0')
            break;
        sscanf(str,"%s %s", s[num].str1, s[num].str2);
        num++;
    }
    sort(s,s+num,cmp);
    while(gets(str)!=NULL)
    {
        if(str[0] == '\0')
            break;
        int ans=binary_find(str);
        if(ans==-1)
            printf("eh\n");
        else
            printf("%s\n",s[ans].str1);
    }
    return 0;
}

3、字典樹

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxl 17
#define maxn 100007
char str[maxl*2],ans[maxl],str1[maxl],str2[maxl];
struct trie
{
    char aim[maxl];
    trie *ne[27];
}*root;
trie *create()
{
    trie *p=new trie;
    for(int i=0;i<27;i++)
        p->ne[i]=NULL;
    strcpy(p->aim,"eh");
    return p;
}
void make_tree(trie *p,char *s,char *e)
{
    int len=strlen(s),k;
    for(int i=0;i<len;i++)
    {
        k=s[i]-'a';
        if(p->ne[k]==NULL)
            p->ne[k]=create();
        p=p->ne[k];
    }
    strcpy(p->aim,e);
}
void find_str(trie *p,char *f)
{
    int len=strlen(f),k;
    for(int i=0;i<len;i++)
    {
        k=f[i]-'a';
        if(p->ne[k]==NULL)
        {
            strcpy(ans,"eh");
            break;
        }
        p=p->ne[k];
    }
    strcpy(ans,p->aim);
}
int main()
{
    root=create();
    int i=0;
    while(true)
    {
        gets(str);
        if(str[0] == '\0')
            break;
        sscanf(str,"%s %s", str1, str2);
        make_tree(root,str2,str1);
        i++;
    }
    while(gets(str)!=NULL)
    {
        if(str[0] == '\0')
            break;
        find_str(root,str);
        printf("%s\n",ans);
    }
    return 0;
}

4、雜湊

ELFHash演算法

unsigned int ELFHash(char *str)  
{  
    unsigned int hash = 0;  
    unsigned int x = 0;  

    while (*str)  
    {  
        hash=(hash << 4);//將hash左移4位,等價於*16
        hash = hash + (*str);
        //加上當前字元位的ASCII碼,即把當前字元ASCII存入hash低四位。   
        if ((x = hash & 0xF0000000L) != 0)   
        //取hash的高四位並賦值給x,如果不為0,則表明字串的長度大於7,現在正在往hash中存入第7個字元,
        //如果再不處理,在新增下一個字元hash左移4位時,曾存入的第一個字元(高4位)就將被移出,造成資訊丟失
        {    
            hash ^= (x >> 24);  
            //將hash的高四位與hash的低5~8位進行異或(算是通過影響5~8位來實現對資訊的一種儲存方式吧<個人理解>),
            //因為hash低1~4剛剛存入了新的字元,所以是右移24位
            //x右移24位時,如果最高的那一位為0,只會影響hash的5-8位,否則會影響5-31位,因為C語言使用的算數移位,
            //最高位為1導致x右移時,會在前面補1 
            hash &= ~x;   //對hash的最高4位(28~31)清零,用於實現對下一個字元的存入
        }  
        str++;  //遍歷下一個字元
    }  
    return (hash & 0x7FFFFFFF);    
    //如果最後結果是負數,處理為非負數,即捨棄最高位(當處理字串時,只有字元不可能為負值)
}  

程式碼實現

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100007
#define maxl 17
const int mod = maxn;
int hashTable[mod];
int countt;
char ans[maxl];
struct HashNode
{
    char str1[maxl];
    char str2[maxl];
    int next;
};
HashNode h[maxn];
void init_Hash()
{
    for (int i = 0; i < mod; i++)
    {
        hashTable[i] = -1;
    }
    countt = 0;
}
int ELFhash(char *str)
{
    unsigned long hash = 0;
    unsigned long x = 0;
    while (*str)
    {
        hash = (hash << 4) + (*str++);
        if ((x = hash & 0xF0000000L) != 0)
        {
            hash ^= (x >> 24);
            hash &= ~x;
        }
    }
    return hash % mod;
}
void insert_Hash(char str1[],char str2[])
{
    int value = ELFhash(str2);
    strcpy(h[countt].str1,str1);
    strcpy(h[countt].str2,str2);
    h[countt].next = hashTable[value];
    hashTable[value] = countt;
    countt++;
}
bool search_Hash(char str[])
{
    int value = ELFhash(str);
    int next = hashTable[value];
    while (next != -1)
    {
        if (strcmp(str, h[next].str2) == 0)
        {
            strcpy(ans,h[next].str1);
            return true;
        }
        next = h[next].next;
    }
    return false;
}
int main()
{
    init_Hash();
    char str1[maxl],str2[maxl];
    char str[maxl*2];
    while(true)
    {
        gets(str);
        if(str[0] == '\0')
            break;
        sscanf(str,"%s %s", str1, str2);
        insert_Hash(str1,str2);
    }
    while (gets(str)!=NULL)
    {
        if(str[0] == '\0')
            break;
        if (search_Hash(str))
            printf("%s\n",ans);
        else
            printf("eh\n");
    }
    return 0;
}

相關推薦

POJ 2503 BabelfishSTL二分字典

Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign langua

Poj百練 2503: Babelfish 分類:二分

一道簡單字串相關的二分題 題中有些OJ中常見的基礎操作,下面是筆記: cin.get() & cin.peek() 前者是觀測並移除輸入流的最後一個字元,類似出棧,經常用來捨棄回車等不需要的操作 後者是隻用來獲取輸入流的最後一個字元 用例:當要求輸入回車

POJ 2503 Babelfishmap,字典,快排+二分,hash

題意:先構造一個詞典,然後輸入外文單詞,輸出相應的英語單詞。 這道題有4種方法可以做: 1.map 2.字典樹 3.快排+二分 4.hash表 思路1:可以使用map來做 程式碼: #include<iostream> #include<stdio.

POJ 2503 Babelfishmap入門

pro getc scan appear std 出現 elf map stream 題目鏈接:http://poj.org/problem?id=2503 代碼: #include<cstdio> #include<string> #in

題解報告:poj 2503 Babelfishmap

cti 格式化 .... 數據 rtu water sequence words end Description You have just moved from Waterloo to a big city. The people here speak an incomp

URAL 1989線段+字串

題意:給一個字串(<=1e5), 進行操作和查詢(<=1e5)。 1)將指定位置的字元改為c 2)詢問l-r的子串,是否是迴文串。 多項式雜湊: Hash[i] = Hash[i - 1] * x + s[i](其中1 < i <= n,Has

資料庫索引B,B+

資料庫索引是儲存引擎用於快速找到記錄的一種資料結構。         《高效能MySQL》 一. 什麼是索引?   

HDU 5649 DZY Loves Sorting二分答案+線段線段合併+線段分割

題意 一個 \(1\) 到 \(n\) 的全排列,\(m\) 種操作,每次將一段區間 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作後的第 \(k\) 位。 \(1 \leq n \leq 10^5\) 思路 兩個 \(\log\) 的做法展現了二分答案的強大功能。首先二分列舉第 \(k

HDU 5649 DZY Loves Sorting二分答案+線段線段合並+線段分割

空間 namespace memset ons \n create name 題意 size 題意 一個 \(1\) 到 \(n\) 的全排列,\(m\) 種操作,每次將一段區間 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作後的第 \(k\) 位。 \(1

python code practice:KMP演算法二分搜尋的實現

1、替換空格 題目描述:請實現一個函式,將一個字串中的每個空格替換成“%20”。例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。 分析: 將長度為1的空格替換為長度為3的“%20”,字串的長度變長。 如果允許我們開闢一個新的陣列來存放替換空格後的字串, 那麼這道

POJ 2503 -- Babelfish

lis pil cst miss ima align 存在 temp cep Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47018 Accepted: 19

poj-2503-Babelfish

理解 地址 per aid RM mar case body 我們 Description You have just moved from Waterloo

POJ-2503 Babelfish---map或者hash

size 容器 main 總數 ace 一個 cst #define 第一個 題目鏈接: https://vjudge.net/problem/POJ-2503 題目大意: 就像查找一本字典,根據輸入的條目和要查詢的單詞,給出查詢結果(每個單詞長度不超過10) 解題思路:

數據結構與算法-二叉斜二叉滿二叉完全二叉線索二叉

大型 結點 develop pac string col 限制 也會 斐波那契數 前言:前面了解了樹的概念和基本的存儲結構類型及樹的分類,而在樹中應用最廣泛的種類是二叉樹 一、簡介   在樹型結構中,如果每個父節點只有兩個子節點,那麽這樣的樹被稱為二叉樹(Binary

演算法導論 第十一章:散列表 筆記直接定址表散列表通過連結法解決碰撞函式開放定址法完全

前面討論的各種資料結構中,記錄在各種結構中的相對位置是隨機的,和在記錄的關鍵字之間不存在有確定的關係,因此在查詢記錄是需要進行一系列和關鍵字的比較。而理想的情況是不希望進行任何的比較,一次存取便能得到所查記錄。那就必須在記錄的儲存位置和它的關鍵字之間建立一種確定的關係f,使每個關鍵字和結構中有一

【Asteroids 】【POJ - 3041】網路流-二分匹配-匈牙利思維

題目: Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contai

POJ 2503 Babelfish(字典)

題意:每個單詞對應一個單詞,然後輸入n個單詞,輸出對應的單詞。 題解:map能解決,排序+二分也能,字典樹也能。水題 程式碼: #include<iostream> #includ

第3章 決策ID3演算法建立繪製決策分類器儲存預測隱性眼鏡型別

ID3演算法 ID3演算法的核心是在決策樹各個結點上對應資訊增益準則選擇特徵,遞迴地構建決策樹。具體方法是:從根結點(root node)開始,對結點計算所有可能的特徵的資訊增益,選擇資訊增益最大的特徵作為結點的特徵,由該特徵的不同取值建立子節點;再對子結點遞迴地呼叫以上方法,構建決策樹

資料結構中常見的BST二叉搜尋AVL平衡二叉RBT紅黑B-B+B*

BST樹 即二叉搜尋樹:        1.所有非葉子結點至多擁有兩個兒子(Left和Right);        2.所有結點儲存一個關鍵字;        3.非葉子結點的左指標指向小於其關鍵字的子樹,右指標指向大於其關鍵字的子樹; 如:      

詳談結構傳統字典hash Merkle Patricia Tree

關於資料結構中樹結構的相關分享 一、傳統的資料結構中的樹結構 樹結構是一種非線性儲存結構,儲存的是具有“一對多”關係的資料元素的集合。 其中,討論較多的是二叉樹。二叉樹的每個結點至多隻有二棵子樹(不存在度大於2的結點),二叉樹的子樹有左右之分,次序不