预处理下连续相等的字符个数其实主要是看是否满3个

后面递推的时候特判下+1上次递推[i-1,j-1]不是来自[i-2,j-1]也不是来自[i-1,j-2]其实就是只来自[i-4,j-4]+3,和[i-2,j-2]+1这样才能保证连续让长度超过3的继续增加1

但是这里不通过直接[i-4,j-4]+3[i-1,j-1]判断因为不满足3的不算进来,比如[0,0]+3[3,3]可以但是[1,1]+3!=[4,4]因为[1,1]也是0,就不能推出[5,5]了,当然也可以用[3,3]+1==[4,4]或判断,不过数组下标-4就又把范围扩大了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication12
{
class Program
{
public static int[,] dp;
public static int[,] pd;
static void Main(string[] args)
{
string nstr;
string mstr;
nstr = Console.ReadLine();
mstr = Console.ReadLine();
int n = nstr.Length;
int m = mstr.Length;
dp = new int[n, m];
pd = new int[n, m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i < 1 || j < 1)
pd[i, j] = (nstr[i] == mstr[j] ? 1 : 0);
else
pd[i, j] = (nstr[i] == mstr[j] ? pd[i - 1, j - 1] + 1 : 0);
for (int i = 2; i < n; i++)
{
for(int j = 2; j < m; j++)
{
if (pd[i, j] >= 3)
{
if (i < 3 || j < 3)
dp[i, j] = 3;
else
dp[i, j] = Math.Max(dp[i, j], dp[i - 3, j - 3] + 3);
}
if(dp[i-1,j-1]!=dp[i-2,j-1]&&dp[i-1,j-1]!=dp[i-1,j-2])
dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j - 1]+(pd[i,j]>=3?1:0));
dp[i, j] = Math.Max(dp[i, j], dp[i - 1, j]);
dp[i, j] = Math.Max(dp[i, j], dp[i, j - 1]);
}
}
Console.WriteLine(dp[n-1,m-1]);
}
}
}

Hihocoder 1059 String Matching Content Length的更多相关文章

  1. String Matching Content Length

    hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...

  2. hiho_1059_string matching content length

    题目大意 两个字符串strA和strB(长度最大为2100),他们中按照顺序有一些公共的子串,且公共子串的长度大于等于3,否则不认为是合法的,比如 abcdef 和 abcxcdef, 按照顺序有合法 ...

  3. WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data

    异常信息:The maximum string content length quota (8192) has been exceeded while reading XML data 问题:调用第三 ...

  4. The maximum string content length quota (8192) has been exceeded while reading XML data

    原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们W ...

  5. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  6. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  7. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

随机推荐

  1. python 爬虫(一)

    1. 一次简单的网页访问 urllib 是一个标准的python库(意味着不需要安装任何附件的东西来运行这个demo),包含了通过网络请求数据的方法,处理cookies,甚至更改metadata比如h ...

  2. inner join on 和 where = 的区别!

    请看下面两条语句:select * from table1 inner join table2 on table1.id = table2.idselect * from table1,table2 ...

  3. Spatial Transformer Networks(空间变换神经网络)

    Reference:Spatial Transformer Networks [Google.DeepMind]Reference:[Theano源码,基于Lasagne] 闲扯:大数据不如小数据 这 ...

  4. Java web servlet 拦截器 以登陆为例子

    以登陆为例子............... public class LoginFilter implements Filter { @Override public void destroy() { ...

  5. js中for in的用法

    for(var i=0;i<len;i++)这样的用法一般都可以用for in 来替代. 例如: var a = ["a","b","c&quo ...

  6. 关于Java异常

    此处讲运行时异常和非运行时异常地区别,并分别举例 运行时异常一般为程序逻辑错误引起的,可选择捕获处理或不处理,如:IndexOutOfBoundException, NullPointerExcept ...

  7. *HDU1150 二分图

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 保护眼睛(ubuntu 和 chrome)

    chrome 安插件https://chrome.google.com/webstore/detail/%E4%BF%9D%E6%8A%A4%E7%9C%BC%E7%9D%9B/fgadnbmmoln ...

  9. IOS网络第三天 - 01-网络文件下载(0922略)

    01 数据的安全01 - 密码加密 02 数据的安全02 - 加密过程 01 -数据的安全01 - 本地存储和代码安全 04-网络状态监控 05-真机演示 06-小文件下载 07-大文件下载01-基本 ...

  10. python 2.43 升级到2.7

    [root@GW1 bin]# lsb_release -aLSB Version: :core-3.1-amd64:core-3.1-ia32:core-3.1-noarch:graphics-3. ...