zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典第五章总结

    #include<iostream>
    #include<cstdio>
    #include<sstream>//stringstream保存在sstream文件中 
    #include<string>
    #include<algorithm>
    using namespace std;
    
    int main(void)
    {
        string line;
        //string 使用getline(cin,str)  char[]使用cin.getline(str,100); 
        while(getline(cin,line))
        {
            int sum=0,x;
            stringstream ss(line); 
            while(ss>>x) sum+=x;
            cout << sum << endl;
        } 
        return 0;
    }
    #include<iostream>
    using namespace std;
    
    struct Point{
        int x,y;
        Point(int x=0,int y=0):x(x),y(y){ }//这里可以初始化操作
      //Point(int x=0,int y=0){ this->x=x; this->y=y; } }; Point
    operator+(const Point&a,const Point&b)//重载运算符 { return Point(a.x+b.x,a.y+b.y); } ostream& operator<<(ostream&out,const Point &b)//重载运算符 { out << "(" << b.x << "," << b.y << ")"; return out; } int main(void) { Point a,b(1,2); a.x=3; cout << a+b << endl; return 0; }

     大理石在哪里

    //自己写的代码
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<vector>
    using namespace std;
    int main(void)
    {
        int n,m,x,time=0;
        while(cin >> n >> m)
        {
            vector<int>v;
            for(int i=0;i<n;i++)
            {
                cin >> x;
                v.push_back(x);    
            } 
            sort(v.begin(),v.end());
            cout << "CASE# " << ++time << ":"<<endl; 
            for(int i=0;i<m;i++)
            {
                cin >> x;
                int p=-1;
                for(int j=0;j<v.size();j++) 
                if(v[j]==x)
                {
                    p=j;
                    break;    
                }
                if(p == -1)cout << x << " not found
    " << endl;
                else  cout << x << " found at " << p+1 << endl;
            }
        }
        return 0;    
    } 
    //思路:先排序,再查找,使用algorithm头文件中的sort和lower_bound
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    const int maxn = 10000;
    
    int main(void)
    {
        int n,q,x,a[maxn],kase;
        while(scanf("%d%d",&n,&q)==2 && n)
        {
            printf("CASE# %d
    ",++kase);
            for(int i=0;i<n;i++) scanf("%d",&a[i]);
            sort(a,a+n);
            while(q--)
            {
                scanf("%d",&x);
                //lower_bound(a,a+n,x)函数返回x应该在的位置 ,lower_bound()函数作用是查找:大于或等于x的第一个位置; 
                int p = lower_bound(a,a+n,x)-a;
                if(a[p]==x) printf("%d found at %d
    ",x,p+1);
                else printf("%d not found
    ",x);
            }    
        }
        return 0;    
    } 

    集合:

    #include<iostream>
    #include<cstring>
    #include<sstream> 
    #include<set>
    #include<algorithm>
    using namespace std;
    int main(void){
        string s,buf;
        set<string>dict;
        while(cin >> s)
        {
            for(int i=0;i<s.length();i++)//isalpha(ch)判断是不是英文字母 ,等价于isupper(ch) || islower(ch) 
                if(isalpha(s[i])) s[i]=tolower(s[i]); else s[i]=' ';
            stringstream ss(s);
            while(ss >> buf) dict.insert(buf);
        }
        for(set<string>::iterator it=dict.begin();it!=dict.end();it++) cout << *it << endl; 
        return 0;
    }

     映射:

    #include<set>
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<vector>
    using namespace std;
    
    map<string ,int >cnt;//使用映射记录相应的字符串的数量,类似使用一个数组记录一个 
    vector<string>words;
    
    string standard(string s)
    {
        for(int i=0;i<s.length();i++)
            if(isalpha(s[i])) s[i]=tolower(s[i]);
        sort(s.begin(),s.end());
        return s;
    }
    
    
    int main(void)
    {
        int n=0;
        string s;
        while(cin >> s)
        {
            if(s[0]=='#') break;
            words.push_back(s);
            string r=standard(s);
            if(!cnt.count(r)) cnt[r]=0;
            cnt[r]++; 
        }
        vector<string>ans;
        for(int i=0;i<words.size();i++)
        {
            if(cnt[standard(words[i])]==1) ans.push_back(words[i]);
        }
        sort(ans.begin(),ans.end());
        for(int i=0;i<ans.size();i++) cout << ans[i] << endl; 
        return 0;
    }

    map 和 set 都支持insert,find,count,remove操作。map也称为关联数组。

    集合栈:

    #include<set>
    #include<string>
    #include<iostream>
    #include<cstdio>
    #include<stack>
    #include<map> 
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    typedef set<int> Set;
    map<Set,int>IDcache; //将一个集合映射为一个整数 
    vector<Set>Setcache; //将集合保存在向量数组中 
    
    int ID(Set x)
    {
        //如果在已经存在这个集合,那么就直接返回这个集合的编号,
        //否则就在集合向量数组中添加 这个集合,并在映射中保存编号 
        if(IDcache.count(x)) return IDcache[x];
        Setcache.push_back(x);
        IDcache[x]=Setcache.size()-1;
        return IDcache[x]; 
    }
    
    stack<int>s;
    
    int main(void)
    {
        int n;
        cin >> n;
        for(int i=0;i<n;i++)
        {
            string op;
            cin >> op;
            if(op[0]=='P') s.push(ID(Set()));    
            else if(op[0]=='D') s.push(s.top());
            else 
            {
                Set x1=Setcache[s.top()]; s.pop();
                Set x2=Setcache[s.top()]; s.pop();
                Set x;
                if(op[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op[0]=='A') {
                    x=x2;
                    x.insert(ID(x1));
                }
                s.push(ID(x));    
            }
        } 
        cout << Setcache[s.top()].size() << endl; 
        return 0;
    } 

    自己重新写了一个,但是没有使用技巧

    #include<iostream>
    #include<vector>
    #include<map>
    #include<set>
    #include<stack> 
    #include<algorithm>
    #include<cstdio>
    #include<string>
    using namespace std;
    
    map<set<int>,int>IDcache;
    vector<set<int> >Setcache;
    
    int ID(set<int> x)
    {
        if(IDcache.count(x)) return IDcache[x];
        Setcache.push_back(x);
        IDcache[x]=Setcache.size()-1;
        return IDcache[x];
    }
    
    stack<int>s;
    
    int main(void)
    {
        int n;
        cin >> n;
        for(int i=0;i<n;i++)
        {
            string op;
            cin >> op;
            if(op[0]=='P') s.push(ID(set<int>()));    
            else if(op[0]=='D') s.push(s.top());
            else 
            {
                set<int> x;
                set<int> x1=Setcache[s.top()]; s.pop();
                set<int> x2=Setcache[s.top()]; s.pop();
                if(op[0]=='U') set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op[0]=='I') set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op[0]=='A'){
                    x=x2; x.insert(ID(x1));
                } 
                s.push(ID(x));
             }
        }
        cout << Setcache[s.top()].size() << endl; 
        return 0;    
    } 

     团队队列

    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<map>
    
    using namespace std;
    
    const int maxn = 1000 + 10;
    
    int main(void)
    {
        int t,kase=0;
        while(scanf("%d",&t)==1 && t)
        {
            printf("Scenario #%d
    ",++kase);
            
            map<int,int>team;
            for(int i=0;i<t;i++)
            {
                int n,x;
                scanf("%d",&n);
                while(n--) {
                    scanf("%d",&x);
                    team[x]=i;
                }
            }
            queue<int> q,q2[maxn];
            for(;;)
            {
                int x;
                char cmd[10];
                scanf("%s",cmd);
                if(cmd[0]=='S') break;
                else if(cmd[0]=='D'){
                    int t=q.front();
                    printf("%d
    ",q2[t].front()); q2[t].pop();
                    if(q2[t].empty()) q.pop();
                }
                else if(cmd[0]=='E')
                {
                    scanf("%d",&x);
                    int t = team[x];
                    if(q2[t].empty()) q.push(t);
                    q2[t].push(x); 
                }
            }
            printf("
    ");
        }
        return 0;
    } 

    丑数:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<set>
    #include<queue>
    using namespace std;
    
    const int a[3]={2,3,5};
    
    typedef long long LL;
    
    int main(void)
    {
        priority_queue<LL,vector<LL>,greater<LL> >pq;
        set<LL> s;
        pq.push(1);
        s.insert(1);
        for(int i=1;;i++)
        {
            LL x=pq.top(); pq.pop();
            if(i==1500){
                cout << x << endl;
                break;
            }
            for(int j=0;j<3;j++)
            {
                LL temp = x*a[j];
                if(!s.count(temp)){
                    s.insert(temp);
                    pq.push(temp);
                }     
            }    
        } 
        return 0;
    }
  • 相关阅读:
    【bzoj3211】花神游历各国
    四维偏序(K-D-Tree+rebuild)
    【bzoj3295】动态逆序对
    BZOJ3444: 最后的晚餐
    BZOJ3091: 城市旅行
    BestCoder Round #72
    BZOJ2733: [HNOI2012]永无乡
    wc2016总结
    BZOJ4381: [POI2015]Odwiedziny
    BZOJ4196: [Noi2015]软件包管理器
  • 原文地址:https://www.cnblogs.com/zuimeiyujianni/p/9029680.html
Copyright © 2011-2022 走看看