http://codeforces.com/contest/355/problem/D

这道题问了一下学妹,难道说哥已经老了!!!

首先题意理解上有些问题 比如说

a   b    c

b   d    e

f    g    h

第一个人写下 a ,第二个人写下 b,然后又该第一个人写了,这时候的 b 既可以是上边的 b

也可以是下边的 b 由将要写的人决定

dp[n][1<<20] 中dp[x][y] 表示的意思是到第x步时,选y这个状态为结尾(所对应的字符都相等)

且前面的字符串都一样时,接下来怎么选出的最优解

代码:

#include<iostream>
#include<stack>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<cmath> using namespace std; typedef long long ll;
typedef pair<int,int> pp;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
const int N=41;
int dp[N][1<<20];
char c[N][N];
int a[N][N];
int n;
int dfs(int x,int y)
{
if(dp[x][y]!=INF)
return dp[x][y];
int w=(y|(y<<1));
int s[26]={0};
for(int h=0,x1=x,y1=1;h<n;++h,x1--,y1++)
if((y&(1<<h))&&x1>=1&&y1<=n)
{dp[x][y]=a[x1][y1];break;}
if(x==2*n-1)
return dp[x][y];
for(int h=0,x1=x+1,y1=1;h<n;++h,x1--,y1++)
if((w&(1<<h))&&x1>=1&&y1<=n)
{
int t=c[x1][y1]-'a';
s[t]=(s[t]|(1<<h));
}
int k0=-INF,k1=INF;
for(int i=0;i<26;++i)
if(s[i]>0)
{
k0=max(k0,dfs(x+1,s[i]));
k1=min(k1,dfs(x+1,s[i]));
}
if((x&1))
dp[x][y]+=k1;
else
dp[x][y]+=k0;
return dp[x][y];
}
int main()
{
//freopen("data.in","r",stdin); while(cin>>n)
{
int m=(1<<n);
for(int i=0;i<N;++i)
for(int j=0;j<m;++j)
dp[i][j]=INF;
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
cin>>c[i][j];
if(c[i][j]=='a') a[i][j]=1;
if(c[i][j]=='b') a[i][j]=-1;
}
int k=dfs(1,1);
//cout<<k<<endl;
if(k>0)
cout<<"FIRST"<<endl;
if(k<0)
cout<<"SECOND"<<endl;
if(k==0)
cout<<"DRAW"<<endl;
}
//fclose(stdin);
return 0;
}

D. Game with Strings的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

  10. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

随机推荐

  1. 24-React Components组件

    Components 组件 1.组件 可以让UI独立的分割出来,可以让UI重复利用. 2.组件就像是JavaScript函数,它们能够接收任意的输入(称为"props",即属性)并 ...

  2. ORACLE常用SQL优化hint语句

    在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...

  3. Bootstrap_面板

    一.基础面板 基础面板非常简单,就是一个div容器运用了“panel”样式,产生一个具有边框的文本显示块. 由于“panel”不控制主题颜色,所以在“panel”的基础上增加一个控制颜色的主题“pan ...

  4. sql-GOTO跳转

    --声明变量 DECLARE @X INT --标记GOTO跳转位置 TEST: PRINT @X --WHILE @X<=3 --GOTO跳转到执行位置 GOTO TEST

  5. JavaScript学习笔记(一):介绍JavaScript的一些简单知识

    JavaScript是世界上最流行的编程语言.这门语言可用于HTML和web,更可广泛用于服务器.PC.笔记本电脑和智能手机等设备.---------------------------------- ...

  6. GOLANG 基本数据类型 整型

    基本数据类型-整型 种类     有符号(负号)      int8 int16 int32 int64 无符号(无符号) uint8 uint16 uint32 uint64 架构特定(取决于系统位 ...

  7. 未解决的问题,登录163邮箱http://mail.163.com/,用xpath的方式定位密码输入框的时候,总是报找不到该元素

    退出的时候出现: xpath定位方法: 注意xpath路径写的太长,如果层级全部写完定位不到,就尝试去掉一些层级

  8. 【所见即所得】textarea 精确限制字数、行数,中、英、全半角混检 。源码带注释

    主要解决难点: 1 中文 全角2字符深深的恶意 2 多行输入textarea 手动,自动换行问题 3 获得每行内容 原创,转载请注明出处! <!DOCTYPE html PUBLIC " ...

  9. mutation annovar

    1.annovar  很全面  http://annovar.openbioinformatics.org: 2.http://blog.goldenhelix.com/:

  10. jquery总结06-动画事件04-自定义动画

    .animate(params,[speed],[easing],[fn]) params,[speed],[easing],[fn]Options,Number/String,String,Func ...