【codeforces 765A】Neverending competitions

【题目链接】:http://codeforces.com/contest/765/problem/A

【题意】

给你一个人的n个行程
行程都是从家到某个地方或从某个地方到家;
且是无序的,且如果到了非家的地方,它的下一个目的地就是家;
问你这n个行程过后这个人在家里还是不在家里;

【题解】

直接模拟这个行程就好;
对于从家到x的,cnt[x]++
对于从x到家的,cnt[x]–;
看看所有的cnt是不是都等于0;
是的话在家里
否则在外面;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

map <string,int> dic;
int n,tot,cnt[N*2];
string s;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    rei(n);
    cin >> s;
    dic[s]=++tot;
    rep1(i,1,n)
    {
        cin >> s;
        int po = s.find("-",0);
        string s1 = s.substr(0,po),s2="";
        int len = s.size();
        rep1(j,po+2,len-1)
            s2+=s[j];
        if (!dic[s1]) dic[s1] = ++tot;
        if (!dic[s2]) dic[s2] = ++tot;
        int a = dic[s1],b = dic[s2];
        if (a==1)
            cnt[b]++;
        else
            if (b==1)
                cnt[a]--;
    }
    rep1(i,1,tot)
        if (cnt[i]!=0)
            return puts("contest"),0;
    puts("home");
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626618.html