HDU 2253 Longest Common Subsequence Again
其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写
#include <stdio.h>
#include <string.h> #define M 30005
#define SIZE 128
#define WORDMAX 3200
#define BIT 32 char s1[M], s2[M];
int nword;
unsigned int str[SIZE][WORDMAX];
unsigned int tmp1[WORDMAX], tmp2[WORDMAX]; void pre(int len)
{
int i, j;
memset(str, , sizeof(str));
for(i = ; i < len; i ++)
str[s1[i]][i / BIT] |= << (i % BIT);
} void cal(unsigned int *a, unsigned int *b, char ch)
{
int i, bottom = , top;
unsigned int x, y;
for(i = ; i < nword; i ++)
{
y = a[i];
x = y | str[ch][i];
top = (y >> (BIT - )) & ;
y = (y << ) | bottom;
if(x < y) top = ;
b[i] = x & ((x - y) ^ x);
bottom = top;
}
} int bitcnt(unsigned int *a)
{
int i, j, res = , t;
unsigned int b[] = {0x55555555, 0x33333333, 0x0f0f0f0f, 0x00ff00ff, 0x0000ffff}, x;
for(i = ; i < nword; i ++)
{
x = a[i];
t = ;
for(j = ; j < ; j ++, t <<= )
x = (x & b[j]) + ((x >> t) & b[j]);
res += x;
}
return res;
} void process()
{
int i, j, len1, len2;
unsigned int *a, *b, *t;
len1 = strlen(s1);
len2 = strlen(s2);
nword = (len1 + BIT - ) / BIT;
pre(len1);
memset(tmp1, , sizeof(tmp1));
a = &tmp1[];
b = &tmp2[];
for(i = ; i < len2; i ++)
{
cal(a, b, s2[i]);
t = a; a = b; b = t;
}
printf("%d\n", bitcnt(a));
} int main()
{
while(scanf("%s%s", s1, s2) != EOF)
process();
return ;
}
2253
HDU 2253 Longest Common Subsequence Again的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
随机推荐
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- SQL使用技巧-批量删除-批量更新-bcp导出-跨服务器sql
1.循环删除数据 while @@rowcount>0 begin delete top (1000) from T where OperateTime >=2014 ...
- 读取XML字符串到临时表
DECLARE @hdoc int DECLARE @doc xml SET @doc = '<CityValueSet> <CityItem> <CityId>2 ...
- HTML提交表单
一.使用form提交表单 <form action="#" method="post"> {% csrf_token %} 班级<input ...
- 【Python常见问题总结】
1. python2 中 end = '' 取消换行没有用 解决办法: 在程序开始加入 from __future__ import print_function 2. 如何在电脑上同时使用pytho ...
- Swagger 生成 PHP API 接口文档
Swagger 生成 PHP API 接口文档 Lumen微服务生成Swagger文档 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文 ...
- HDU——T 1068 Girls and Boys
http://acm.hdu.edu.cn/showproblem.php?pid=1068 Time Limit: 20000/10000 MS (Java/Others) Memory Li ...
- hadoop-05-mysql修改密码
hadoop-05-mysql修改密码 su root 1,service mysqld start 2,vi /var/log/mysqld.log #在这里面查找密码 3, mysql -uroo ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- POJ1338 & POJ2545 & POJ2591 & POJ2247 找给定规律的数
POJ1338 2545 2591 2247都是一个类型的题目,所以放到一起来总结 POJ1338:Ugly Numbers Time Limit: 1000MS Memory Limit: 10 ...