D. Game with Strings
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的更多相关文章
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
随机推荐
- html5的新特性——拖放API
在HTML5之前只能使用鼠标事件模拟出"拖放"效果:HTML5专门为拖放提供了7个事件句柄. 被拖动的源对象可以触发的事件: (1)ondragstart:源对象开始被拖动 (2 ...
- JavaScript parseInt() 函数
定义和用法 parseInt() 函数可解析一个字符串,并返回一个整数. 语法 parseInt(string, radix) 参数 描述 string 必需.要被解析的字符串. radix 可选.表 ...
- maven添加sqlserver的jdbc驱动包
http://search.maven.org/中没有sqlserver的jdbc驱动,所以需要本地安装sqljdbc的jar包,然后再在pom里面引入 Step 1 在微软官网下载sqljdbc的j ...
- GOLANG 声明
GO语言声明关键字 var 声明变量 const 声明常量 type 声明新类型 func 声明函数(方法) package 声明包 interface 声明接口
- Java - 安全的退出线程
stop() 存在的问题 使用 stop() 来退出线程是不安全的.它会解除由线程获取的所有锁,可能导致数据不一致. 举个例子: public class StopTest { public stat ...
- JFinal 的初始化
浅析初始化过程 首先要从 web 容器进行初始化 <?xml version="1.0" encoding="UTF-8"?> <web-ap ...
- javascript控制cookie
参考:http://www.cnblogs.com/ly312/archive/2010/07/14/1777190.html function getCookies(name) { var arr ...
- table中的th td margin不生效
margin-top,margin-left,margin-right,margin-bottom 是body中的属性th,td 是表格的元素与之对应的是padding-top,padding-lef ...
- 7 -- Spring的基本用法 -- 5...
7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...
- 使用input range滑块,控制元素transform rotate旋转样式
<!DOCTYPE html> <html> <head> </head> <body> <!-- 第一步:设置div旋转对象和inp ...