AtCoder - 2153 An Ordinary Game list模拟 || 博弈

http://abc048.contest.atcoder.jp/tasks/arc064_b?lang=en

在vj里面用list模拟水过去了,然后感觉vj不靠谱,上atcoder交,果然tle

我的思路是这样的,first那个是没有必胜策略的,赢就是赢,随便拿哪一个都是赢。

所以只需要找到有多少个能拿的就行了。用list模拟下,TLE.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#include <list>
list<char>theList;
const int maxn = 1e5 + 20;
char str[maxn];
void work() {
    scanf("%s", str + 1);
    int lenstr = strlen(str + 1);
    for (int i = 1; i <= lenstr; ++i) {
        theList.push_back(str[i]);
    }
//    for (list<char> :: iterator it = theList.begin(); it != theList.end(); ++it) {
//        cout << *it;
//    }
    int ans = 0;
    while (theList.size() > 2) {
        if (theList.size() == 3 && theList.front() == theList.back()) break;
        list<char> :: iterator itBegin = theList.begin();
        itBegin++;
        list<char> :: iterator itEnd = theList.end();
        itEnd--;
        list<char> :: iterator itBeginPre = theList.begin();
        list<char> :: iterator itBeginNext = itBegin; itBeginNext++;
        bool flag = false;
        while (itBegin != itEnd) {
            if (*itBeginPre == *itBeginNext) {
                itBeginPre++;
                itBegin++;
                itBeginNext++;
            } else {
                theList.erase(itBegin);
                flag = true;
                break;
            }
        }
        if (!flag) break;
        ans++;
    }
//    cout << ans << endl;
    if (ans & 1) {
        cout << "First" << endl;
    } else cout << "Second" << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code

 

那么就需要快速找到有多少东西能拿,一般情况下,能拿的东西是lenstr - 2个,减去头和尾

博弈:

分为两种情况:

1、头和尾相等,这个时候能拿的东西次数 - 1,

2、头和尾不等,拿的东西次数不变

然后判断奇偶性就好。

那么死锁怎么办?就是像ababa

这里能拿的个数是0,但是按照上面的,是2.

看看基本的死锁是:aba,这样,那么再添加2个,可以形成新的死锁ababa

注意到添加元素的个数必定要是偶数,这个不影响奇偶性。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#include <list>
list<char>theList;
const int maxn = 1e5 + 20;
char str[maxn];
void work() {
    scanf("%s", str + 1);
    int lenstr = strlen(str + 1);
    lenstr -= 2;
    if (str[1] == str[strlen(str + 1)]) lenstr--;
    if (lenstr & 1) {
        cout << "First" << endl;
    } else cout << "Second" << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code

 

posted on 2017-03-13 13:57  stupid_one  阅读(201)  评论(0编辑  收藏  举报

导航