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 ...
随机推荐
- Android TextView加上阴影效果
<TextView android:id="@+id/test_shadow" android:layout_width="wrap_content" a ...
- jquery 几种类选择器方式
代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestClas ...
- 【原创】TimeSten安装与配置
1.安装TimeSten 2.安装时要指定TNS_ADMIN_LOCATION,即tnsnames.ora的路径,因为tt会根据这个连接Oracle.C:\TimesTen\tt1122_32\net ...
- Mojo For Chromium Developers
Overview This document contains the minimum amount of information needed for a developer to start us ...
- .is() 全选复选的判断
/* 全选/全不选 */function selectAll(){ if($("#ckAll").is(":checked",true)){ $(": ...
- SpaceVim配置中遇到的问题
这是一个不断更新的随笔,若遇到SpaceVim配置问题时,会添加项 字体乱码(linux企鹅乱码,tabline图标乱码等) git clone https://github.com/powerlin ...
- 洛谷 P1373 小a和uim之大逃离 (差值型dp总结)
这道题和多米诺骨牌那道题很像 ,都是涉及到差值的问题. 这道题是二维的,同时要取模. 这种题,因为当前的决策有后效性,会影响到差值,所以直接把 差值作为维度,然后计算答案的时候把差值为0的加起来就行了 ...
- 常见WEB错误代码
404表示文件或资源未找到 java WEB常见的错误代码 1.1xx-信息提示:这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协 ...
- Mysql导入Sql文件时报Error Code: 2013 - Lost connection to MySQL server during query
MySql 有时我们导入sql文件,文件过大,导致Error Code: 2013 - Lost connection to MySQL server during query这种错误 执行以下: S ...
- Qt之滚动字幕
简述 滚动字幕,也就是传说中的跑马灯效果. 简单地理解就是:每隔一段时间(一般几百毫秒效果较佳)显示的文字进行变化(即滚动效果). 简述 实现 效果 源码 实现 利用定时器QTimer,在固定的时间 ...