HDU 4679 String
String
Total Submission(s): 695 Accepted Submission(s): 254
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.
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.
aaaaa
aaaa
aa
abcdef
acebdf
cf
Case #2: 3
For test one, D is "aaaa", and for test two, D is "acf".
#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的更多相关文章
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 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 ...
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4679 Terrorist’s destroy 树形DP
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...
- hdu 5772 String problem 最大权闭合子图
String problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5772 Description This is a simple pro ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- HDU 2476 String painter(区间DP+思维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...
- 2017多校第6场 HDU 6096 String AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...
- HDU 6194 string string string(后缀数组+RMQ)
string string string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
随机推荐
- JAVA中List、Map、Set的区别与选用
类层次关系如下: Collection ├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap └WeakHashM ...
- 禁止apache显示目录索引
1)修改目录配置: 复制代码 代码如下: <Directory "D:/Apache/blog.phpha.com">Options Indexes FollowSym ...
- poj 3128 Leonardo's Notebook(置换的幂)
http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,能够把它看做一个置换.推断这个置换是否是某个置换的平方. 思路:具体解释可參考url=ihxG ...
- GDB调试之暂停
暂停机制: 有3种方式可以通知GDB暂停程序的执行. a.断点: 通知GDB在程序中的特定位置暂停执行: b.监视点:通知GDB当特定内存位置(或者涉及一个或多个位置的表达式)的值发生变化时暂停执行: ...
- 常用类库StringBuilder
1.使用StringBuilder拼接字符串实例: string[] lines = File.ReadAllLines("sdjk.txt", Encoding.Default) ...
- 使用uWSGI+nginx部署Django项目
最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...
- MYSql和PHP计算数据性能
MYSQL不是有很多内部计算函数吗? 比如我要计算一列数据的平均值,,那我是直接用MYSQL里面的函数在SQL语句中计算 快 :还是用SQL把数据取出来了,用PHP计算快呢?(SQL语句PHP语句都用 ...
- 我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)
一.面向对象基础 面向对象名词解释: 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公 ...
- 在Visual Studio 2010中安装iGraph
本安装步骤参考了:http://www.16kan.com/question/detail/182225.html Igraph actually does work with Visual C++ ...
- 数据切分——Mysql分区表的建立及性能分析
Mysql的安装方法可以参考: http://blog.csdn.net/jhq0113/article/details/43812895 Mysql分区表的介绍可以参考: http://blog.c ...