竹園論壇

標題: a174 - 上帝玩不玩骰子? [打印本頁]

作者: Sylveon    時間: 2014-5-6 13:12
標題: a174 - 上帝玩不玩骰子?
原題:http://zerojudge.tw/ShowProblem?problemid=a174
AC:http://zerojudge.tw/Submissions?problemid=a174&account=lfs92002

HASH Table的小練習~,就照題意模擬的簡單題

/**********************************************************************************/
/*  Problem: a174 "上帝玩不玩骰子?" from Hash Table                      */
/*  Language: CPP (1117 Bytes)                                                    */
/*  Result: AC(0.4s, 524KB) judge by this@ZeroJudge                               */
/*  Author: lfs92002 at 2013-03-20 11:44:18                                       */
/**********************************************************************************/


#include<cstdio>
#include<vector>
#include<algorithm>
#include<set>
#define hash(X) ((X)%M)
using namespace std;
vector<int>ht[200];
set<int>inp;
int main()
{
    int K,M,i,j,t;
    while(~scanf("%d%d",&K,&M))
    {
        for(int a=0;a<M;a++)ht[a].clear();
        inp.clear();
        while(K--)
        {
            scanf("%d",&t);
            switch(t)
            {
                case 1:
                    scanf("%d",&i);
                    if(inp.find(i)==inp.end())
                    {
                        ht[hash(i)].push_back(i);
                        inp.insert(i);
                    }
                    
                    break;
                case 2:
                    scanf("%d",&i);
                    if(inp.find(i)!=inp.end())
                    {
                        inp.erase(i);
                        int d=0;
                        while(ht[hash(i)][d]!=i)d++;
                        while(d<ht[hash(i)].size()-1)ht[hash(i)][d]=ht[hash(i)][d+1],d++;
                        ht[hash(i)].resize(ht[hash(i)].size()-1);
                    }
                    break;
                    
                case 3:
                    puts("===== s =====");
                    for(i=0;i<M;i++)
                    {
                        printf("[%03d]:",i);
                        sort(ht[i].begin(),ht[i].end());
                        for(j=0;j<ht[i].size();j++)
                        {
                            printf("%d -> ",ht[i][j]);
                        }
                        printf("NULL\n");
                    }
                    puts("===== e =====");
                    break;
            }
            
        }
    }
}







歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2