String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 695    Accepted Submission(s): 254


Problem Description
Given 3 strings A, B, C, find the longest string D which satisfy the following rules:

a) D is the subsequence of A

b) D is the subsequence of B

c) C is the substring of D

Substring here means a consecutive subsequnce.

You need to output the length of D.
 
Input
The first line of the input contains an integer T(T = 20) which means the number of test cases.

For each test case, the first line only contains string A, the second line only contains string B, and the third only contains string C.

The length of each string will not exceed 1000, and string C should always be the subsequence of string A and string B.

All the letters in each string are in lowercase.

 
Output
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
 
Sample Input
2
aaaaa
aaaa
aa
abcdef
acebdf
cf
 
Sample Output
Case #1: 4
Case #2: 3

Hint

For test one, D is "aaaa", and for test two, D is "acf".

 
Source
 
>Recommend
zhuyuanchen520
   
  
    祭奠一下这个题
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#define N 1100
using namespace std;
string s1,s2,s3,s4,s5;
char temp[N];
int num1[N][N],num2[N][N];
struct num
{
int sta,end;
} a[N],b[N];
int main()
{
//freopen("data.in","r",stdin);
void get(int (*p)[N],string ch1,string ch2);
int t,tem=1;
scanf("%d",&t);
while(t--)
{
cin>>s1>>s2>>s3;
get(num1,s1,s2);
int l1 = s1.size();
for(int i=0; i<=l1-1; i++)
{
temp[l1-1-i] = s1[i];
}
temp[l1] = '\0';
s4 = temp;
int l2 = s2.size();
for(int i=0; i<=l2-1; i++)
{
temp[l2-1-i] = s2[i];
}
temp[l2] = '\0';
s5 = temp;
get(num2,s4,s5);
int l3 = s3.size(),Top1=0,Top2=0;
for(int i=0; i<=l1-1; i++)
{
if(s1[i]==s3[0])
{
int x = 0;
int sta = i;
int end = -1;
for(int j=i; j<=l1-1&&x<=l3-1; j++)
{
if(s1[j]==s3[x])
{
x++;
}
if(x==l3)
{
end = j;
}
}
if(end==-1)
{
continue;
}
a[Top1].sta = sta;
a[Top1++].end = end;
}
}
for(int i=0; i<=l2-1; i++)
{
if(s2[i]==s3[0])
{
int x = 0;
int sta = i;
int end = -1;
for(int j=i; j<=l2-1&&x<=l3-1; j++)
{
if(s2[j]==s3[x])
{
x++;
}
if(x==l3)
{
end = j;
}
}
if(end==-1)
{
continue;
}
b[Top2].sta = sta;
b[Top2++].end = end;
}
}
int res = l3,Max=0;
for(int i=0; i<=Top1-1; i++)
{
for(int j=0; j<=Top2-1; j++)
{
int x1 = a[i].sta;
int y1 = a[i].end;
int x2 = b[j].sta;
int y2 = b[j].end;
int k1 = 0;
if(x1>0&&x2>0)
{
k1 = num1[x1-1][x2-1];
}
int k2 = 0;
if(y1<l1-1&&y2<l2-1)
{
k2 = num2[l1-2-y1][l2-2-y2];
}
Max = max(Max,res+k2+k1);
}
}
printf("Case #%d: %d\n",tem++,Max);
}
return 0;
}
void get(int (*p)[N],string ch1,string ch2)
{
int l1 = ch1.size();
int l2 = ch2.size();
memset(p,0,sizeof(p));
for(int i=0; i<=l1-1; i++)
{
for(int j=0; j<=l2-1; j++)
{
if(i==0&&j==0)
{
if(ch1[i]==ch2[j])
{
p[i][j] = 1;
}
}
else if(i==0&&j!=0)
{
if(ch1[i]==ch2[j])
{
p[i][j] = 1;
}
else
{
p[i][j] = p[i][j-1];
}
}
else if(i!=0&&j==0)
{
if(ch1[i]==ch2[j])
{
p[i][j] = 1;
}
else
{
p[i][j] = p[i-1][j];
}
}
else
{
if(ch1[i]==ch2[j])
{
p[i][j] = p[i-1][j-1]+1;
}
else
{
p[i][j] = max(p[i][j-1],p[i-1][j]);
}
}
}
}
}

HDU 4679 String的更多相关文章

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

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

  2. Terrorist’s destroy HDU - 4679

    Terrorist’s destroy HDU - 4679 There is a city which is built like a tree.A terrorist wants to destr ...

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

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu 4679 Terrorist’s destroy 树形DP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...

  5. hdu 5772 String problem 最大权闭合子图

    String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...

  6. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

  7. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  8. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  9. HDU 6194 string string string(后缀数组+RMQ)

    string string string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

随机推荐

  1. webrtc教程

    cdsn博客不支持word文件,所以这里显示不完全.可到本人资源中下载word文档: v0.3:http://download.csdn.net/detail/kl222/6961491 v0.1:h ...

  2. JAVA中SSH面试问题

    1.阐述struts2的执行流程. Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器Filter ...

  3. Android应用--简、美音乐播放器增加音量控制

    Android应用--简.美音乐播放器增加音量控制 2013年6月26日简.美音乐播放器继续完善中.. 题外话:上一篇博客是在6月11号发的,那篇博客似乎有点问题,可能是因为代码结构有点乱的原因,很难 ...

  4. http协议与http代理

    TCP/IP协议族 TCP/IP(Transmission Control Protocol/InternetProtocol.传输控制协议/网际协议)是用于计算机通信的一个协议族. TCP/IP协议 ...

  5. poj2342 Anniversary party【树形dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316097.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  6. python命令行参数处理

    使用argparse包来解析命令行参数: #/usr/bin/python #encoding=utf-8 import argparse parser = argparse.ArgumentPars ...

  7. MYSQL大小写(由于数据由windows迁移到Linux导致)

    今日从sqlserver上迁移了一个数据库到Linux的MySQL中,迁移成功了,但是应用却跑不通,查看日志发现,提示找不到表,我注意到,表名都是存在大小写的,而MySQL中的表名都是小写的.这提醒了 ...

  8. Spring boot实现数据库读写分离

    背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...

  9. 快速提取PROTEL99SE PCB文件上的封装方法

    1.首先打开你要提取元件封装的PCB. 2.执行生成元件库的命令...软件会帮你把这个PCB上的所有元件生成一个临时库. 3.打开你自己的元件库... 4.PCB刚才生成的元件库中选中你所需要的元件, ...

  10. QTcpSocket通信编程时阻塞与非阻塞的问题

    目标,qt程序作为客户端,windows下winsock作为服务器端,实现两端通信. 开始时写了一个小函数测试: [cpp] view plaincopy QTcpSocket tmpSock;  t ...