URAL 1287. Mars Canals 滚动数组+DP

求从上到下 从左到右 从左上到右下 从右上到左下的 最长的S和s串

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1444;
char a[maxn][maxn];
int dp[2][maxn][2][4];
int main()
{
	int n;
	int ans1 = 0, ans2 = 0;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%s", a[i]+1);
	int l = 1, r = l^1;
	for(int i = 1; i <= n; i++)
	{
		l = r;
		r = l^1;
		for(int j = 1; j <= n; j++)
		{
			if(i > 1 && a[i][j] == a[i-1][j])
			{
				if(a[i][j] == 'S')
					dp[l][j][0][0] = dp[r][j][0][0]+1;
				else
					dp[l][j][1][0] = dp[r][j][1][0]+1;
			}
			else
			{
				if(a[i][j] == 'S')
					dp[l][j][0][0] = 1;
				else
					dp[l][j][1][0] = 1;
			}
			if(j > 1 && a[i][j] == a[i][j-1])
			{
				if(a[i][j] == 'S')
					dp[l][j][0][1] = dp[l][j-1][0][1]+1;
				else
					dp[l][j][1][1] = dp[l][j-1][1][1]+1;
			}
			else
			{
				if(a[i][j] == 'S')
					dp[l][j][0][1] = 1;
				else
					dp[l][j][1][1] = 1;
			}
			if(i > 1 && j > 1 && a[i][j] == a[i-1][j-1])
			{
				if(a[i][j] == 'S')
					dp[l][j][0][2] = dp[r][j-1][0][2]+1;
				else
					dp[l][j][1][2] = dp[r][j-1][1][2]+1;
			}
			else
			{
				if(a[i][j] == 'S')
					dp[l][j][0][2] = 1;
				else
					dp[l][j][1][2] = 1;
			}
			if(i > 1 && j < n && a[i][j] == a[i-1][j+1])
			{
				if(a[i][j] == 'S')
					dp[l][j][0][3] = dp[r][j+1][0][3]+1;
				else
					dp[l][j][1][3] = dp[r][j+1][1][3]+1;
			}
			else
			{
				if(a[i][j] == 'S')
					dp[l][j][0][3] = 1;
				else
					dp[l][j][1][3] = 1;
			}

			ans1 = max(max(ans1, dp[l][j][0][0]), max(dp[l][j][0][1], dp[l][j][0][2]));
			ans1 = max(ans1, dp[l][j][0][3]);
			ans2 = max(max(ans2, dp[l][j][1][0]), max(dp[l][j][1][1], dp[l][j][1][2]));
			ans2 = max(ans2, dp[l][j][1][3]);
			//printf("%d %d\n", ans1, ans2);
		}
	}
	if(ans1 > ans2)
		printf("%c %d\n", 'S', ans1);
	else if(ans1 < ans2)
		printf("%c %d\n", 's', ans2);
	else
		printf("%c %d\n", '?', ans1);
	return 0;
}
时间: 2024-08-27 09:46:08

URAL 1287. Mars Canals 滚动数组+DP的相关文章

Palindrome_滚动数组&amp;&amp;DP

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

poj - 1159 - Palindrome(滚动数组dp)

题意:一个长为N的字符串( 3 <= N <= 5000),问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->>状态:dp[i][j]表示第i个字符到第j个字符组成的字符串变成回文串的最少插入次数. 状态转移方程: 若sz[i] == sz[j],则:dp[i][j] = dp[i + 1][j - 1]; 否则:dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1; 提交,5

ural 2018. The Debut Album 滚动数组dp

点击打开链接 2018. The Debut Album Time limit: 2.0 second Memory limit: 64 MB Pop-group "Pink elephant" entered on recording their debut album. In fact they have only two songs: "My love" and "I miss you", but each of them has a la

HDU 4576 简单概率 + 滚动数组DP(大坑)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576 坑大发了,居然加 % 也会超时: 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include <cstdlib> 7 #include <string>

【滚动数组】【状压dp】Gym - 100956F - Colored Path

f(i,j,S)表示到(i,j),且经由的路径上的颜色集合为S的价值的最小值,从上方和左方转移过来即可. 要注意,内存不足,需要滚动数组优化,即使用了map,还是需要. 路径输出的时候,可以再跑一遍dp,这样就不用再开一个大数组了. 我的写法比较菜.卡了常数 #include<cstdio> #include<algorithm> #include<map> using namespace std; int w[401][401],c[401][401]; typede

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

HDU 3392 Pie(DP+滚动数组)

题意:有一些男生女生,男生女生数量差不超过100 ,男生女生两两配对.要求求出一种配对方法,使每一对的高度差的和最小. 思路:(我是真的笨笨笨!!磨磨唧唧写一堆是因为我笨!我看了别人的博客,思路全是学别人的,轻喷!)设人少的一组人数为n,b[],人多的一组人数为m,g[](b[],g[]先排好序),用dp[i][j]表示n中的前i个人与m中的前j个人配对所得到的最小值. 那么dp[i][j]就是min(dp[i-1][k]+|b[i]-g[k]|),就是n中前i-1个人和m中前1~k个人配对的最

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

vijos 1907 DP+滚动数组

描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告失败. 为了简化问题,我们对游戏规则进行了简化和改编: 游戏界面是一个长为 n,高为 m 的二维平面,其中有k 个管道(忽略管道的宽度). 小鸟始终在游戏界面内移动.小鸟从游戏界面最左边 任意整数高度位置出发,到达游戏界面最右边时,游戏完成. 小鸟每个单位时间沿横坐标方向右移的距离为 1,竖直移动的