设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA。

设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB。

求出每一对合法的(stA, edA),(stB, edB)

对每一组( stA[i], edA[i] ) 和 ( stB[j], ed[j] ), 求串A[0,stA[i]-1]和串B[0, stB[j]-1]的最长公共子序列长度L1, 求串A[edA[i]+1, lenA-1]和串B[stB[j]+1, lenB-1]的最长公共子序列长度L2, 答案为max( lenC+L1+L2 )……

现在看看自己赛场上写的代码觉得自己好脑残啊……虽然不紧张但是当时在TLE了N次之后已经觉得脑子是一团浆糊了,我从机子上下静下心仔细想想的时候才发现我根本没理解透薛薛的意思。下次觉得自己不好了的时候一定要果断换人,不然太耽误时间了orz……

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1010 using namespace std; int N;
int c1[maxn][maxn];
int c2[maxn][maxn];
int duiA[maxn][];
int duiB[maxn][];
int cntA, cntB;
char strA[maxn], strB[maxn], strC[maxn];
char ReA[maxn], ReB[maxn];
int lenA, lenB, lenC; void Calc(char* a, char* b, int c[maxn][maxn])
{
int i,j;
int la=strlen(a);
int lb=strlen(b);
for(i=; i<la; i++)
c[i][]=;
for(j=; j<lb; j++)
c[][j]=;
for(i=; i<=la; i++)
{
for(j=; j<=lb; j++)
{
if(a[i-]==b[j-])
c[i][j]=c[i-][j-]+;
else
{
if(c[i][j-]>c[i-][j])
c[i][j]=c[i][j-];
else
c[i][j]=c[i-][j];
}
}
}
return;
} int solved()
{
int tmpans = ; for ( int i = ; i < cntA; ++i )
for ( int j = ; j < cntB; ++j )
{
int preA = duiA[i][] - , preB = duiB[j][] - ;
int sufA = lenA - duiA[i][];
int sufB = lenB - duiB[j][];
//printf("c1[%d][%d]=%d c2[%d][%d]=%d\n", preA, preB, c1[preA][preB], duiA[i][1], duiB[j][i], c2[sufA][sufB] );
int tmp = ;
if ( preA > && preB > ) tmp += c1[preA][preB];
if ( sufA > && sufB > ) tmp += c2[sufA][sufB];
tmpans = max( tmpans, tmp );
} return tmpans;
} void preSolved()
{
cntA = ;
cntB = ; for ( int i = ; i + lenC < lenA; ++i )
{
if ( strA[i] != strC[] ) continue;
int cur = i + , j;
for ( j = ; j < lenC && cur < lenA; )
{
if ( strA[cur] == strC[j] ) ++j;
++cur;
}
if ( j == lenC )
{
duiA[cntA][] = i + ;
duiA[cntA][] = cur;
++cntA;
}
} for ( int i = ; i + lenC < lenB; ++i )
{
if ( strB[i] != strC[] ) continue;
int cur = i + , j;
for ( j = ; j < lenC && cur < lenB; )
{
if ( strB[cur] == strC[j] ) ++j;
++cur;
}
if ( j == lenC )
{
duiB[cntB][] = i + ;
duiB[cntB][] = cur;
++cntB;
}
} return;
} int main()
{
//freopen("s.txt", "w", stdout );
int T, cas = ;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", strA );
scanf( "%s", strB );
scanf( "%s", strC );
lenA = strlen( strA );
lenB = strlen( strB );
lenC = strlen( strC );
if ( lenC == lenA || lenC == lenB )
{
printf( "Case #%d: %d\n", ++cas, lenC );
continue;
}
Calc( strA, strB, c1 );
for ( int i = lenA - ; i >= ; --i ) ReA[lenA--i] = strA[i];
for ( int i = lenB - ; i >= ; --i ) ReB[lenB--i] = strB[i];
ReA[lenA] = '\0', ReB[lenB] = '\0';
Calc( ReA, ReB, c2 ); preSolved();
int ans = solved() + lenC;
printf( "Case #%d: %d\n", ++cas, ans );
}
return ;
}

HDU 4681 String 胡搞的更多相关文章

  1. HDU 4681 String 最长公共子序列

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...

  2. hdu 4681 string

    字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...

  3. HDU 4681 String(2013多校8 1006题 DP)

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  4. HDU 4681 STRING dp+暴力。

    题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...

  5. hdu 4681 String(转载)

    #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...

  6. HDU 4681 String(DP)

    题目链接 枚举A和B中每一段含有C的段,A的前面 后面和B前面后面,求最长公共子序.观察发现,可以预处理最长公共子序. #include <iostream> #include <c ...

  7. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  8. HDU 4690 EBCDIC (2013多校 1005题 胡搞题)

    EBCDIC Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Su ...

  9. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. CUDA数组分配

    原问链接 概述:数组分配可以通过cudaMallocArray()和cudaMalloc3DArray() 1.cudaMallocArray() cudaError_t cudaMallocArra ...

  2. ajax模仿form上传图片

    <form id="iconForm"> <input class="js_upFile cover1" type="file&qu ...

  3. mybatis中oracle转mysql

    刚来公司实习,遇到的第一个任务就是这个,简单记录一下思路过程.人菜的很,没啥参考价值. 测试时: 将现有的oracle库转为mysql: 用的Navicat自带数据传输功能,简单粗暴 出现的问题: 1 ...

  4. percona-zabbix-templates插件安装监控MySQL

    前期准备:被监控机已经安装好php 1.在zabbix客户端安装mysql监控插件rpm包 rpm -ivh https://www.percona.com/downloads/percona-mon ...

  5. python--Matplotlib(二)

    Matplotlib+pandas作图 一.对csv文件进行提取ruixi.csv 对上述表格进行提取并做图 #-*- coding:utf-8 -*- import matplotlib as mp ...

  6. Python全栈day 04

    Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...

  7. C语言进阶——enum, sizeof, typedef 分析11

    枚举类型的使用方法: enum是C语言的 一种自定义类型 enum值是可以根据需要自定义的整型值 第一个enum的值默认为0 默认情况下的enum值是在前一个定义值的基础上加 1 enum类型的变量只 ...

  8. POJ:3262-Protecting the Flowers

    Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8606 Accepted: 347 ...

  9. [bzoj1552][Cerc2007]robotic sort&&[bzoj3506][Cqoi2014]排序机械臂

    非常垃圾的一道平衡树,结果被日了一天.很难受嗷嗷嗷 首先不得不说网上的题解让我这个本来就不熟悉平衡树的彩笔很难受——并不好理解. 还好Sinogi大佬非常的神,一眼就切掉了,而且用更加美妙的解法. 题 ...

  10. hdu1251统计难题(trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...