题目大意

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

删字符的规则如下:

  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. [BTS] Error biztalk arguments null exception string reference not set to an instance of a string. parameter name

    biztalk arguments null exception string reference not set to an instance of a string. parameter name ...

  2. 阻塞队列--LinkedBlockingQueue

    什么叫线程安全?线程安全就是每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的. 线程安全就是说多线程访问同一代码,不会产生不确定的结果. 并行和并发区别1.并行是指两者同时 ...

  3. Redis教程(八):事务详解

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/135.html?1455806987 一.概述: 和众多其它数据库一样,R ...

  4. Java程序员的日常 —— static的用法讲解实践

    之前文章说过Java中static的作用,有朋友想看个例子.于是便抽空写了个小栗子 代码 package xing.test.thinking.chap5; class A{ public A() { ...

  5. 使用paramikoHelper类实现MySQL安装和数据恢复

    本脚本实现远程Linux主机登陆和安装MySQL的rpm包,同时导入mysql数据,实现自动化安装 paramikoHelper类在本博客中 http://www.cnblogs.com/djoker ...

  6. Nodejs学习笔记(十一)--- 数据采集器示例(request和cheerio)

    目录 写在之前 示例 示例要求 采集器 加入代理 请求https 写在之后... 写在之前 很多人都有做数据采集的需求,用不同的语言,不同的方式都能实现,我以前也用C#写过,主要还是发送各类请求和正则 ...

  7. paip.基于urlrewrite的反向代理以及内容改写

    paip.基于urlrewrite的反向代理以及内容改写 ---------反向代理 RewriteCond %{REQUEST_URI} !=/process.php RewriteRule  ^( ...

  8. 修改JSONArray里所有key的值

    下面举一个代码的列子目的是实现如下功能: [{"userId":1,"userName":"plf"},{"userId" ...

  9. Spring基于注解及SpringMVC

    1.使用注解 (1)组件扫描 指定一个包路径,Spring会自动扫描该包 及其子包所有组件类,当发现组件类定义前有 特定的注解标记时,就将该组件纳入到Spring 容器.等价于原有XML配置中的< ...

  10. 使用AJAX填充<select>标签下拉项,没有显示指定的option项

    newCarInfo.js代码如下: $(function() {     // 获取燃油种类     url = "basicFuelType_queryAll.action"; ...