Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional board. At the start of the game all cells in the board are empty. In each turn a player puts an X in an empty cell, and if the move results three X next to each other, that player wins.

Given the current state of the game, you are to determine if the current player to move can win the game assuming both players play optimally.

Consider the game where the board size is 5 cells. If the first player puts an X at position three (in the middle) so the state becomes ..X.., he will win the game as no matter where the other player puts his X, the first player can get three X in a row. If, on the other hand, the first player puts the X in any other position, the second player will win the game by putting the X in the opposite corner (for instance, after the second players move the state might be .X..X). This will force the first player to put an X in a position so the second player wins in the next move.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing a string denoting the current status of the game. The string will only contain the characters '.' and 'X'. The length of the string (the size of the board) will be between 3 and 200 characters, inclusive. No state will contain three X in a row.

Output

For each case, print the case number and the positions on the board, where the player to move may put an X and win the game. The positions should be separated by a single space, and be in increasing order. The leftmost position on the board is 1. If there is no such position print 0.

Sample Input

4

.....

X.....X..X.......X....X..X

.X.X...X

..................

Sample Output

Case 1: 3

Case 2: 0

Case 3: 3

Case 4: 5 6 13 14

题解:题目意思就是给你一行字符串由 '.'和'X'组成,然后两个人交替将一个‘.’

变成'X'.如果某个人先形成连续的3个‘X’,则这个人就取得胜利。问先手必胜的位置是否存在,

如果存在,有多少个,并依次输出其位置;这题肯定是枚举每一个位置,判断是否可以胜利,

对于SG[x]表示长度为x的‘.’区间的SG值,然后对于每一个位置(不是'X'的位置),判断其由

‘.’变成'X'之后是否可以形成连续3个'X'的必胜状态,是否会形成.XX或XX.或X.X的必败状态;如果都不是

再去枚举每一个区间的SG值,再将其异或ans,就得到这一个位置的SG值,为0必胜,不为零必败;

参考代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
typedef long long ll;
#define clr(a,val) memset(a,val,sizeof(a))
const int maxn=;
int T,SG[maxn],len;
vector<int> v;
char s[maxn],s1[maxn];
int getSG(int m)
{
if(m<) return ;
if(SG[m]!=-) return SG[m];
bool vis[maxn];clr(vis,);
for(int i=;i<=m;++i) vis[getSG(i-)^getSG(m-i-)]=;
int t=;
while(vis[t]) ++t;
return SG[m]=t;
} bool check(int x)
{
strcpy(s1,s);
if(s1[x]=='X') return ;
s1[x]='X';
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X'&&s1[i+]=='X') return ;}
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X') return ;}
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X') return ;}
int j=-,f=,ans=;
for(int i=;i<len;++i)
{
if(s1[i]=='X')
{
if(f) ans^=getSG(i-j-);
else ans^=getSG(i-j-),f=;
j=i;
}
}
ans^=getSG(len-j-);
return ans==;
} int main()
{
scanf("%d",&T);
memset(SG,-,sizeof SG);
for(int cas=;cas<=T;++cas)
{
scanf("%s",s);
v.clear();
len=strlen(s);
for(int i=;i<len;++i)
{
if(check(i)) v.push_back(i+);
}
printf("Case %d:",cas);
if(v.size())
{
for(int i=;i<v.size();++i) printf(" %d",v[i]);
puts("");
}
else printf(" 0\n");
} return ;
}

LightOJ 1229 Tablecross的更多相关文章

  1. LightOJ 1229 Treblecross(SG函数打表 + 遍历)题解

    题意:给你一串含“.”和“X”的字串,每次一个玩家可以把‘."变成“X”,谁先弄到三个XXX就赢.假如先手必赢,输出所有能必赢的第一步,否则输出0. 思路:显然如果一个X周围两格有X那么肯定 ...

  2. lightoj 1229 - Treblecross 博弈论

    思路:SG函数 枚举先手的每一个位置是否有必胜. 1)如果出现了XXX则必胜: 2)如果出现了XX或X.X则必败: 3)否则计算后手的sg值和. 代码如下: #include<iostream& ...

  3. 爆零后的感受外加一道强联通分量HDU 4635的题解

    今天又爆零了,又是又,怎么又是又,爆零爆多了,又也就经常挂嘴边了,看到这句话,你一定很想说一句””,弱菜被骂傻,也很正常啦. 如果你不开心,可以考虑往下看. 翻到E(HDU 4635 Strongly ...

  4. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  5. LightOj 1298 - One Theorem, One Year(DP + 欧拉)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...

  6. 1214 - Large Division -- LightOj(大数取余)

    http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...

  7. LightOJ Beginners Problems 部分题解

    相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...

  8. Codevs 1229 数字游戏

    1229 数字游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver     题目描述 Description Lele 最近上课的时候都很无聊,所以他发明了 ...

  9. LightOJ 1341 唯一分解定理

    Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld &a ...

随机推荐

  1. Ansible之常用模块(二)

    1.hostname:此模块的主要作用是管理远端节点主机名 模块帮助: root@localhost ~]# ansible-doc -s hostname - name: Manage hostna ...

  2. Oracle instant client免安装Oracle客户端配置

    不想安装几个G的完整版client,可以直接通过安装包安装的时候选择instant client,如果没有安装包,也可以直接去官网下载一个即时客户端,64位的windows包大小只有78MB左右 传送 ...

  3. Spark性能优化指南——基础篇(转)

    [转]Spark性能优化指南——基础篇 http://mp.weixin.qq.com/s?__biz=MjM5NDMwNjMzNA==&mid=2651805828&idx=1&am ...

  4. 领扣(LeetCode)交替位二进制数 个人题解

    给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等. 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出 ...

  5. 逆向libbaiduprotect(三)- 移植python操作dalvik虚拟机c++函数,配合gdb控制程序运行流程

    python编译移植到测试机,并且移植ctypes模块.利用ctypes代替c程序,利用dalvik内部c++函数,在运行过程中手动命令操控dalvik虚拟机,并结合gdb进行调试.绕过zygote和 ...

  6. opencv 7 直方图与匹配

    图像直方图概述 直方图的计算与绘制 计算直方图:calcHist()函数 找寻最值:minMaxLoc()函数 示例程序:绘制H-S直方图 #include "opencv2/highgui ...

  7. python中的__str__和__repr__方法

    如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class A(object): def __init__(self,name,age): self.name=name se ...

  8. 神奇的 SQL 之 MySQL 性能分析神器 → EXPLAIN,SQL 起飞的基石!

    前言 开心一刻 某人养了一头猪,烦了想放生,可是猪认识回家的路,放生几次它都自己回来了.一日,这个人想了个狠办法,开车带着猪转了好多路进山区放生,放生后又各种打转,然后掏出电话给家里人打了个电话,问道 ...

  9. 页面加载和图片加载loading

    准备放假了!也是闲着了 ,就来整理之前学到或用到的一下知识点和使用内容,这次记录的是关于加载的友好性loading!!!这里记录一下两种加载方法 1.页面加载的方法,它需要用到js里面两个方法 doc ...

  10. nginx支持https配置

    nginx证书 nginx.conf配置.