poj 2503

這個題吧,我是水過去的,然而,一看有些想法真的不錯



1MAP除了輸入之外,沒其他要注意的了

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           100005
#define   MAXN          10000005
#define   maxnode       500005
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair

const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-8;
const int    mod   = 100007;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/

map<string,string>M;
int main(){
    M.clear();
    //char stro[15],strf[15];
    char a[30],b[15],c[15];

    while(gets(a)&&a[0]!='\0')
    {
        sscanf(a,"%s%s",&b,&c);
        if(!M.count(c))M[c]=b;
    }
    while(~scanf("%s",&a)){
        getchar();
        if(M.count(a))cout<<M[a]<<endl;
        else cout<<"eh"<<endl;
    }

}


2qsort+二分查找

這個要注意的是qsort中的cmp的寫法,剛開始不會寫,一直在找

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           100005
#define   MAXN          10000005
#define   maxnode       500005
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair

const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = 3.141592653589793;
const double inf   = 1e18;
const double eps   = 1e-8;
const int    mod   = 100007;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/
typedef struct word{
    char  e[15],f[15];
}word;
word Q[100010];
int cnt;
//int cmp(const void *a, const void *b)
//{
   // return strcmp((*(Entry *)a).f, (*(Entry *)b).f);
//}
int cmp(const void  *a,const void *b){

    return strcmp((*(word*)a).f,(*(word*)b).f);

}

int Search(char p[])
{
    int L=0,R=cnt-1,Mid;
    int t;
    while(L<=R){
        Mid=(L+R)/2;

        t=strcmp(Q[Mid].f, p);
        if(t==0)return Mid;
        else if(t==1)R=Mid-1;
        else L=Mid+1;
    }
    return -1;
}
int main(){
    char a[30],b[15],c[15];
    cnt=0;
    while(gets(a)&&a[0]!='\0')
    {
        sscanf(a,"%s%s",&b,&c);
        strcpy(Q[cnt].e,b);
        strcpy(Q[cnt++].f,c);

    }
    qsort(Q,cnt,sizeof(word),cmp);
    while(~scanf("%s",&a)){
        getchar();
        int pos=Search(a);
        if(pos==-1)cout<<"eh"<<endl;
        else cout<<Q[pos].e<<endl;
    }

}




3hash

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章