zoukankan      html  css  js  c++  java
  • Uva101-STL模拟

    一道有点复杂的STL模拟题,对STL迭代器不太熟悉改了好久,最后总算A了出来。

    感觉用数组更方便。。。但是为了练习STL嘛

    对比白书上的代码,我写的还是傻了点。一开始没有理解四个操作的意思,单纯的模拟。

    #include <algorithm>
    #include <cstring>
    #include <ctype.h>
    #include <cstdlib>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    
    using namespace std;
    
    int N,M,T;
    vector <int> v[30];
    
    int getpos(int x)
    {
        for(int i=0;i<N;i++)
        {
            vector <int>::iterator it = v[i].begin();
            it = find(v[i].begin(),v[i].end(),x);
            if(it != v[i].end())
            {
                return i;
            }
        }
        return -1;
    }
    
    void repose(int x,int a)
    {
        for(vector<int>::iterator it = v[x].begin();it != v[x].end();++it)
        {
            if(*it == a)
            {
                if(++it == v[x].end()) return;
                for(vector<int>::iterator it2 = it;it2 != v[x].end();++it2) 
                {
                    int k = *it2;
                    vector<int>::iterator beg = v[k].begin();
                    //printf("%d %d
    ",k,(int)v[k].size());
                    //-    while(1);
                    v[k].insert(beg,k);
                }
                v[x].erase(it,v[x].end());
                break;
            }
        }
        return;
    }
    
    void move_onto(int a,int b)
    {
        int x = getpos(a),y = getpos(b);
        repose(x,a);repose(y,b);
        for(vector<int>::iterator it = v[x].begin();it != v[x].end();++it)
        {
            if(*it == a)
            {
                v[x].erase(it);
                break;
            }
        }
        for(vector<int>::iterator it = v[y].begin();it != v[y].end();++it)
        {
            if(*it == b)
            {
                v[y].push_back(a);    
                break;
            }
        }
        return;
    }
    
    void move_over(int a,int b)
    {
        int x = getpos(a),y = getpos(b);
        repose(x,a);
        for(vector<int>::iterator it = v[x].begin();it != v[x].end();++it)
        {
            if(*it == a)
            {
                v[x].erase(it);
                break;
            }
        }
        v[y].push_back(a);
        return;
    }
    
    void pile_over(int a,int b)
    {
        int x = getpos(a),y = getpos(b);
        for(vector<int>::iterator it = v[x].begin();it != v[x].end();++it)
        {
            if(*it == a)
            {
                for(vector<int>::iterator it2 = it;it2 != v[x].end();++it2) v[y].push_back(*it2);
                v[x].erase(it,v[x].end());
                break;
            }
        }
        return;
    }
    
    void pile_onto(int a,int b)
    {
        int x = getpos(a),y = getpos(b);
        repose(y,b);
        for(vector<int>::iterator it = v[x].begin();it != v[x].end();++it)
        {
            if(*it == a)
            {
                for(vector<int>::iterator it2 = it;it2 != v[x].end();++it2) v[y].push_back(*it2);
                v[x].erase(it,v[x].end());
                break;
            }
        }
        return;
    }
    
    int main()
    {
        while(~scanf("%d",&N))
        {
            int a,b;
            char op[10],s[10];
            for(int i=0;i<N;i++) {v[i].clear();v[i].push_back(i);}
    
            while(scanf("%s",op) && op[0] != 'q')
            {
                scanf("%d %s %d",&a,s,&b);
                if(getpos(a) == getpos(b)) continue;
                if(!strcmp(op,"move") && !strcmp(s,"onto")) move_onto(a,b);
                else if(!strcmp(op,"move") && !strcmp(s,"over")) move_over(a,b);
                else if(!strcmp(op,"pile") && !strcmp(s,"onto")) pile_onto(a,b);
                else if(!strcmp(op,"pile") && !strcmp(s,"over")) pile_over(a,b);
            }
            for(int i=0;i<N;i++)
            {
                printf("%d:",i);
                for(int j = 0;j < (int)v[i].size();j++) printf(" %d",v[i][j]);
                printf("
    ");
            }
        }
    }
  • 相关阅读:
    【安徽集训】fiend
    【安徽集训】Emerald
    【安徽集训】Entropy
    【安徽集训】字符串
    【福建集训】果树
    【CF335 E】Counting Skyscrapers
    【BZOJ 3514】Codechef MARCH14 GERALD07 加强版
    【未知来源】Randomized Binary Search Tree
    【ZJOI 2016】旅行者
    【AGC002 E】Candy Piles
  • 原文地址:https://www.cnblogs.com/helica/p/4787786.html
Copyright © 2011-2022 走看看