题目大意

两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏

删字符的规则如下:

  1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心

  2. 删掉该字符,同时,他选择的那个字符串分成了两个独立的字符串

现在问,先手是否必胜,如果先手必胜,输出第一步应该删掉第几个字符,有多解的话,输出序号最小的那个

字符串的长度不超过5000,只包含小写英文字母

做法分析

可以这样考虑:将所有的长度大于等于 3(其实只需要找长度为 3 的就行)的奇回文串的中心标记出来

我们将连续的中心视为一个片段,那么,显然,游戏是进行在片段上面的,所以,游戏被分解成了多个子游戏的和

对于每一个片段(子游戏),我们考虑它的 sg 函数,显然,sg 函数只和这个片段的长度有关,于是定义状态 sg[len] 表示长度为 len 的片段的 sg 值。怎么得到当前状态的下一子状态呢?

  如果删掉的是片段的两端,子状态分成了一个长度为 0 和长度为 len-2 的子片段

  如果删掉的是片段的中间,子状态分成了一个长度为 i 和长度为 len-3-i 的子片段

这样,暴力出 sg 值,再依次的枚举第一次删掉的字符就可以解决这题了

参考代码

 #include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int N=; char buff[N];
int sg[N]; int GET_SG(int len) {
if(sg[len]!=-) return sg[len];
bool vs[N];
memset(vs, , sizeof vs);
vs[GET_SG(len-)]=;
for(int i=; i+i<len; i++) vs[GET_SG(i-)^(GET_SG(len--i))]=;
for(int i=; i<N; i++) if(!vs[i]) return sg[len]=i;
} int hehe(int L, int R) {
int sum=;
for(int i=L+; i<R; i++) if(buff[i-]==buff[i+]) {
int id=i;
while(i<R && buff[i+]==buff[i-]) i++;
sum^=sg[i-id];
}
return sum;
} int main() {
memset(sg, -, sizeof sg);
sg[]=, sg[]=;
for(int i=; i<N; i++) if(sg[i]==-) GET_SG(i);
while(scanf("%s", buff)!=EOF) {
bool flag=;
for(int i=, len=(int)strlen(buff); i<len-; i++) {
if(buff[i-]!=buff[i+]) continue;
if(hehe(, i-)^hehe(i+, len-)) continue;
printf("First\n%d\n", i+);
flag=;
break;
}
if(flag) printf("Second\n");
}
return ;
}

E. Playing with String

题目链接 & AC 通道

Codeforces Round #184 (Div. 2) E. Playing with String

Codeforces Round #184 (Div. 2) E. Playing with String(博弈)的更多相关文章

  1. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  2. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  3. Codeforces Round #184 (Div. 2)

    A. Strange Addition (目前的做法好像做烦了) 统计数的\(mask\),表示个.十.百位上是否是0,共8种数. 枚举8种数组成的所有情况\(2^8\),记录最大数量. B. Con ...

  4. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  5. Codeforces Round #522 (Div. 2) C. Playing Piano

    C. Playing Piano 题目链接:https://codeforces.com/contest/1079/problem/C 题意: 给出数列{an},现在要求你给出一个数列{bn},满足: ...

  6. Codeforces Round #296 (Div. 2) A. Playing with Paper

    A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an o ...

  7. 水题 Codeforces Round #296 (Div. 2) A. Playing with Paper

    题目传送门 /* 水题 a或b成倍的减 */ #include <cstdio> #include <iostream> #include <algorithm> ...

  8. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  9. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

随机推荐

  1. Linux:Linux 重要人物

    1.Ken Thompson:C 语言之父和 UNIX 之父 2.Dennis Ritchie:C 语言之父和 UNIX 之父 3.Stallman:著名黑客,GNU 创始人,开发了 Emacs.gc ...

  2. [备忘][转]rsync使用时的常见问题

    sync使用时的常见问题: 错误1: rsync: read error: Connection reset by peer (104) rsync error: error in rsync pro ...

  3. SQL——神奇代码1之Update

    说明:一个带有update的循环的代码.很简单,但是在QQ群里问了,应该说是很少有人注意这个问题,也就是很少有人真的理解SQL中的Update. 代码如下: if object_id('tempdb. ...

  4. java web开发 图片上传功能

    基本思路在于,配置路径,然后用java I/O的api将图片上传到该目录下. String photoPath =    ServletActionContext.getServletContext( ...

  5. Redis教程(九):主从复制配置实例

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/136.html 一.Redis的Replication: 这里首先需要说明 ...

  6. paip.数组以及集合的操作uapi java php python总结..

    paip.数组以及集合的操作uapi 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/att ...

  7. iOS开发-友盟分享使用(2)

    1.友盟SDK提供功能:分享喜欢的东西到新浪微博.qq空间.为微信朋友圈等等等等社交圈. 2.友盟分享前期准备 (1)注册账号 去官网 (2)创建应用获取appkey 类似5556a53667e*** ...

  8. Leetcode 160 Intersection of Two Linked Lists 单向链表

    找出链表的交点, 如图所示的c1, 如果没有相交返回null. A:             a1 → a2                               ↘               ...

  9. php读取大文件的方法

    1.使用file 函数直接读取 $starttime = microtime_float(); ini_set('memory_limit','-1'); $file = "testfile ...

  10. CentOS7安装mysql数据库

    安装完Centos7,迫不急待的想安装mysql数据库,却没想到走了很多弯路,后来经过查资料,才知道了在Centos7中用MariaDB代替了mysql数据库. 准确来说,本文的标题有点误导的意思,本 ...