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. onSaveInstanceState的执行时机

    当某个activity变得"容易"被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK ...

  2. Apache配置文件讲解

        持续作用扩展自 HTTP/1.0 和 HTTP/1.1 的长连接特性.提供了长效的 HTTP 会话,用以在同 一个 TCP 连接中进行多次请求. 在某些情况下, 这样的方式会对包含大量图片的 ...

  3. 理清C++常量指针和指针常量这团乱麻

    写在前面: 与其说C++中的常量指针和指针常量是一块很有嚼头的语法糖,不如说它是一块相当难啃的骨头.其实本来没什么,这无非是const int *p与int* const p的区别, 但一涉及到起名字 ...

  4. PHPExcel对于Excel中日期和时间类型的处理

    PHPExcel是一款优秀的处理Excel文件读写的开源PHP Library,能够给我们提供强大的Excel读写能力,本文针对Excel处理过程中关于日期和时间类型的处理进行深入的讨论.PHPExc ...

  5. 如果你恨一个程序员 忽悠他去做iOS开发(戏谑篇)

    .state { text-align: right; height: 16px; line-height: 16px; color: #999; padding-top: 5px; overflow ...

  6. 新课程开始的第二天,HTML基础制作

    天正式开始学习HTML的基础制作,有简单的指令开始入手. 第一天的学习,因为基础,所以觉得还算简单,主要是对网页背景.图片.文字.表格等的编辑,和一部分链接的使用. 由下面的的一个事例,通过所学的简单 ...

  7. Less环境搭建

    1.在页面中加入 .less 样式表的链接,并将 rel 属性设置为 "stylesheet/less": <link rel="stylesheet/less&q ...

  8. VS.Net 2015 Update3 学习(1) 支持Webpack

    让vs.net 编译的时候自动执行webpack 首先 管理员模式下打开 “Developer Command Prompt for VS2015", 是管理员模式啊! 然后进入 cd c: ...

  9. android面试题

    1. 请描述一下Activity 生命周期. 答: 如下图所示.共有七个周期函数,按顺序分别是: onCreate(), onStart(), onRestart(), onResume(), onP ...

  10. AngularJS小知识点一

    AngularJS是由谷歌公司及一个由开发者组成的个人社区共同打造.其主要优势在于帮助使用者在web应用程序中实现必要的动态视图.它是通过原生的MVC(模型-视图-控制器)功能来增强HTML. PS: ...