1. 程式人生 > >Poj百練 2503: Babelfish (分類:二分)

Poj百練 2503: Babelfish (分類:二分)

一道簡單字串相關的二分題

題中有些OJ中常見的基礎操作,下面是筆記:

cin.get() & cin.peek()

前者是觀測並移除輸入流的最後一個字元,類似出棧,經常用來捨棄回車等不需要的操作

後者是隻用來獲取輸入流的最後一個字元

用例:當要求輸入回車作為結尾,但輸入內容中包含回車時

while(true): 無限迴圈  for(;;)一樣

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

struct Entry{
    char english[11];
    char foreign[11];
}entries[100005];

int Cmp(Entry entry1, Entry entry2 )
{
    return strcmp(entry1.foreign, entry2.foreign) < 0;
}

int main()
{
    int num = 0;
    char word[11];
    while(true){
        scanf("%s%s", entries[num].english, entries[num].foreign);
        num++;
        cin.get();      //去掉行尾的換行符
        if(cin.peek() == '\n')    //檢視是否空行
            break;
    }

    sort(entries, entries+num, Cmp);//按照Cmp中的規則進行排序

    while(scanf("%s",word) != EOF)
    {
        int left = 0, right = num-1;
        int n = 0;
        //二分查詢
        while( left <= right){
            int mid = left + (right-left)/2;
            n = strcmp(entries[mid].foreign, word);
            if(n < 0)
                left = mid+1;
            else if(n > 0)
                right = mid-1;
            else{
                printf("%s\n",entries[mid].english);
                break;
            }
        }
        if(n)
            printf("eh\n");
    }
    return 0;
}

相關推薦

Poj 2503: Babelfish 分類二分

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

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 Babelfishmap

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

POJ 2503 Babelfishmap入門

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

poj 2765基礎題有難度

感覺這道題有點難度 如果單純換算成整數,記錄變數值的長度是不夠的 double 真是牛X 跟我想的不一樣啊 注意printf 裡面 % m.n  g printf格式控制真心麻煩 別人寫的這個很不錯 double真心好 #include <stdio.h> #i

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

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

18.06.30 POJ 1745:Divisibility

ted 描述 來源 str %d std fir src lap 描述 Consider an arbitrary sequence of integers. One can place + or - operators between integers in the s

18.06.30 POJ 2192:Zipper

img string IE osi AC and origin AI XA 描述 Given three strings, you are to determine whether the third string can be formed by combining t

POJ 2456:Aggressive cows

描述 Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positi

poj 2808:校門外的樹

總時間限制:1000ms記憶體限制:65536kB 描述某校大門外長度為L的馬路上有一排樹,每兩棵相鄰的樹之間的間隔都是1米。我們可以把馬路看成一個數軸,馬路的一端在數軸0的位置,另一端在L的位置;數軸上的每個整數點,即0,1,2,……,L,都種有一棵樹。 馬路上有一些區域要用來建地鐵,這些區域用它們在數

poj 2797(基礎題

求簡單,直接用暴力求解方法 對於字典樹不夠熟悉,題目意思有點奇特 #include <fstream> #include <string.h> #include <stdio.h> #include <algorithm>

POJ 2980:大整數乘法

#include <stdio.h> #include <memory.h> using namespace std; #define MaxNum 210 int a[MaxNum]; int b[MaxNum]; char chara[MaxNu

poj 3735 Training little cats 矩陣快速冪

log ack make .cn code little logs 矩陣快速冪 style 題目鏈接: http://poj.org/problem?id=3735 題意: 有n只貓咪,開始時每只貓咪有花生0顆,現有一組操作,由下面三個中的k個操作組成:

poj 1236 Network of Schoolstarjan縮點

problem lan struct http tor tar sch con vector 題目鏈接:http://poj.org/problem?id=1236 題意:給出n個學校和一些學校之間的網絡鏈接關系,學校之間的網絡是單向邊,讓你求出兩個問題的答案,1.至少需

POJ 2288 Islands and Bridges狀壓dp

pen clas tdi pac pre 初始 路徑 nds queue http://poj.org/problem?id=2288 題意: 有n個島嶼,每個島嶼有一個權值V,一條哈密頓路徑C1,C2,...Cn的值為3部分之和: 第1部分,將路徑中每個島嶼的權值累

POJ 1426 Find The MultipleDFS,BFS

ons pro sum 數字 there lin queue hat 一個數 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal

做微信公眾號時,移動端滑動效果swiper插件displaynone顯示滑動問題

其中 display 顯示 width ren 按鈕 沒有 描述 公眾號  微信公眾號的制作,其中缺少不了希望實現標題、內容、圖片的滑動效果, 這時候可以選擇使用(swiper插件)實現相應效果,功能十分強大,鏈接:http://www.swiper.com.cn/

關聯模型1n

time mes 解決 可能 spa 關聯 tle 模式 this 關聯模型 (1對n) ThinkPHP5.0 的關聯采用了對象化的操作模式,你無需繼承不同的模型類 只是把關聯定義成一個方法,並且直接通過當前模型對象的屬性名獲取定義的關聯數據。 關聯定義:一個user

Vmware workstation安裝體驗Ubuntu18.04ByJohnny

station 沒有 http ubunt 自己 image tool AD 最小   昨天Canonical發布了最新Ubuntu18.04 TLS,迫不及待就安裝來體驗一下。 需要工具: VMware Workstation Ubuntu18.04 iso鏡像 下載鏈接

java基礎17、集合類1Collection

實例 array 9.png str 列表 println 方式 elements .com 一.  什麽是集合類? 二.  Collection接口   2.1  Collection子接口(List接口和Set接口)   2.1  Collection的常用方法 三.