CodeForces 404D Minesweeper 1D (DP)
题意:给定一个序列,*表示雷,1表示它旁边有一个雷,2表示它旁边有两个雷,0表示旁边没有雷,?表示未知,求有多少情况。
析:dp[i][j] 表示第 i 个放 j 状态,有多少种情况,然后很简单的DP就可以搞定。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL dp[2][5];
char s[maxn];
/*
0 - 0
1 - *1
2 - 1*
3 - *2*
4 - *
*/ int main(){
cin >> s+1;
n = strlen(s+1);
int cnt = 0;
if(s[1] == '0') dp[cnt][0] = 1;
else if(s[1] == '1') dp[cnt][2] = 1;
else if(s[1] == '*') dp[cnt][4] = 1;
else if(s[1] == '?') dp[cnt][0] = dp[cnt][2] = dp[cnt][4] = 1;
cnt ^= 1; for(int i = 2; i <= n; ++i, cnt ^= 1){
memset(dp[cnt], 0, sizeof dp[cnt]);
if(s[i] == '0')
dp[cnt][0] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
else if(s[i] == '1'){
dp[cnt][1] = dp[cnt^1][4];
dp[cnt][2] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
}
else if(s[i] == '2')
dp[cnt][3] = dp[cnt^1][4];
else if(s[i] == '*')
dp[cnt][4] = (dp[cnt^1][2] + dp[cnt^1][3] + dp[cnt^1][4]) % mod;
else {
dp[cnt][0] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
dp[cnt][1] = dp[cnt^1][4];
dp[cnt][2] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
dp[cnt][3] = dp[cnt^1][4];
dp[cnt][4] = (dp[cnt^1][2] + dp[cnt^1][3] + dp[cnt^1][4]) % mod;
}
} LL ans = dp[cnt^1][0] + dp[cnt^1][1] + dp[cnt^1][4];
cout << ans % mod << endl;
return 0;
}
CodeForces 404D Minesweeper 1D (DP)的更多相关文章
- Codeforces 404D Minesweeper 1D
题意: 给定字符串,其中'*'表示地雷,'1'表示左/右边有一个地雷相邻,'2'表示左右两边均有地雷相邻,'0'表示左右均无地雷相邻,'?'表示待定,可填入0,1,2或者地雷,有多少种表示方法使字母串 ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- 【codeforces 404D】Minesweeper 1D
[题目链接]:http://codeforces.com/problemset/problem/404/D [题意] 让你玩一个1维的扫雷游戏; 游戏的描述由数字0..2以及符号*表示; 分别表示这个 ...
- Codeforces 404D [DP]
/* 我是一个习惯后悔,但是没办法忍受内疚感的二货== 这题是个无脑dp,但是比赛大概20min没出...其实最后5min我好好想想简单化边界条件,可以出的. 题意: 给你一个长度为1e6的由?*01 ...
- codeforces Minesweeper 1D
题意:就是挖地雷,给你一个字符串,‘*’代表地雷,‘1’代表在它的周围有1个地雷,‘2’代表在左右都有个地雷,‘?’代表不确定是不是地雷,可以是1,2,*,问你最后有几种方式确定所有的的地雷. 思路: ...
- Codeforces Global Round 1D(DP,思维)
#include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){ ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- Codeforces 786C. Till I Collapse 主席树
题目大意: 给定一个长度为\(n\)的序列,要求将其划分为最少的若干段使得每段中不同的数字的种数不超过\(k\). 对于 \(k = 1 .. n\)输出所有的答案. \(n \leq 10^5\) ...
- 冒泡算法-bubble
冒泡算法在数据只有几个无序时是最快的算法,但是如果全部无序的话就变成了最慢的算法了,时间复杂度为O(n^2) public class bubbleSort { public static void ...
- 【转】Java内存与垃圾回收调优
要了解Java垃圾收集机制,先理解JVM内存模式是非常重要的.今天我们将会了解JVM内存的各个部分.如何监控以及垃圾收集调优. Java(JVM)内存模型 正如你从上面的图片看到的,JVM内存被分成多 ...
- linux内核图形配置疑难解决
配置linux内核是遇到的问题:(1)问题一make gconfig * * Unable to find the GTK+ installation. Please make sure that * ...
- poj 3463 Sightseeing——次短路计数
题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...
- Apache Htpasswd生成和验证密码
Assuming you create the password using the following command and "myPassword" as the passw ...
- 【转】Inter-eNB S1 切换
本博客介绍Inter-eNB的S1切换的流程 当eNB收到测量报告,或是因为内部负荷分担等原因,触发了切换判决,进行eNB间小区间通过S1口的切换. 源eNB通过S1接口的 HANDOVER REQU ...
- Annotation之四:注解中的-Xlint:unchecked和 -Xlint:deprecation
一.-Xlint:unchecked用法 对如下Test.java编译时 package com.dxz.annotation; import java.util.ArrayList; import ...
- 生产环境该如何选择lvs的工作模式,和哪一种算法
lvs的工作模式有这几种: 1.RR : 轮叫算法,平均分配,你一个,我一个: 2.WRR :加权轮叫算法,谁的处理能力强,谁的权重就高: 3.LC :最少链接算法,谁的连接数最少,谁就处理更多的链接 ...
- Mysql数据库服务器配置文件/etc/my.cnf的详细配置
以下是 Mysql数 据库服务器配置文件 /etc/my.cnf的详细配置.应用场合是 InnoDB引擎, 4核 CPU, 32位SUSE. [client] port = 3306 ...