预处理下连续相等的字符个数其实主要是看是否满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. containing block

    1(position:static和relative) 它的包含块由它最近的块级.单元格(table cell)或者行内块(inline-block)祖先元素创建. 2.position:fixed ...

  2. 【CodeVS2800】 送外卖 最短路+状压DP

    首先求出各点之间的最短路,floyed即可,注意是0-n. 然后考虑状压,f[i][j]表示状态为i时访问j点时的最短路和,1表示访问,0表示未访问,然后第j个点所在的位置就是(1<<j) ...

  3. AT指令(中文详解版)(一)

    一 . 一 般 命 令1.AT+CGMI      给出模块厂商的标识.2.AT+CGMM    获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800    或PCS 1900) ...

  4. ZeroMQ接口函数之 :zmq_errno – 返回errno的值给调用此函数的线程

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_errno zmq_errno(3)         ØMQ Manual - ØMQ/3.2.5 Name zm ...

  5. MongoDB基本使用

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  6. NOSQL场景梳理

    Redis 场景:缓存,Session,消息发布订阅,产品属性分析,订单购买等强事务,计数等   Memcached 场景:读密集,写一般的缓存,Session   MongoDB 场景:数据显示,查 ...

  7. PHP编程效率的20个要点

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数” 用 单引号代替双引 ...

  8. JAVA实现复制文件夹

    package com.filetest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  9. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  10. jsp标签<c:forEach>取出传递参数注意

    运行书里的代码,其中servlet可以通过以下两个方式向jsp传参数: 1.         request.getSession().setAttribute("productList&q ...