记codeforces两题

A。http://codeforces.com/contest/653/problem/A

错在for(int i=0; i<v.size()-2; i++)  //当v.size()<2时,v.size()-2非负。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<iostream>

using namespace std;

int main(){
    int n;
    while(scanf(" %d", &n)==1){
        vector<int> v;
        bool have=false;
        for(int i=0; i<n; i++){
            int t;  scanf(" %d", &t);
            v.push_back(t);
        }
      //  cout<<v.size()<<endl;
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
      //  cout<<v.size()<<endl;
        for(int i=0; i<v.size()-2; i++){
            int t0=v[i], t1=v[i+1], t2=v[i+2];
            if(abs(t0-t1)<3 && abs(t1-t2)<3 && abs(t2-t0)<3){
                have=true;   break;
            }
        }
        cout<<(have?"YES":"NO")<<endl;
    }
    return 0;
}

B。http://codeforces.com/contest/653/problem/B

状态不超过6^6次方种,暴力即可,

#stl很好用。

#include<stdio.h>
#include<string>
#include<iostream>

using namespace std;

int n, q;
string s[36];

int count(const string cur){
    if(cur.size()==n)   return 1;
    int cnt=0;
    for(int i=0; i<q; i++){
        if(cur[0]!=s[i][3]) continue;
        string st(cur);
        st.replace(0, 1, s[i].substr(0,2));
        cnt+=count(st);
    }
    return cnt;
}

int main(){
    while(cin>>n>>q){
        getchar();
        for(int i=0; i<q; i++){
            getline(cin, s[i]);
  //          cout<<s[i]<<endl;
        }
        cout<<count("a")<<endl;
    }
    return 0;
}

  

posted @ 2016-03-19 20:44  Ramanujan  阅读(302)  评论(0编辑  收藏  举报