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 ...
随机推荐
- Codeforces 845A. Chess Tourney 思路:简单逻辑题
题目: 题意:输入一个整数n,接着输入2*n个数字,代表2*n个选手的实力. 实力值大的选手可以赢实力值小的选手,实力值相同则都有可能赢. 叫你把这2*n个选手分成2个有n个选手的队伍. ...
- MySQL学习(四)——外键
1.比方现在有两张表“分类表”和“商品表”,为了表明商品属于哪个分类,通常我们将在商品表上添加一列,用于存放分类cid的信息,此列称为:外键. 此时分类表category称为主表,cid称为主键:商品 ...
- String Comparison(C#)
When comparing programmatic strings, you should always use StringComparison.Ordinal or StringCompari ...
- hiho 1620 - 股票价格3 - 无限制的单调队列?
题目链接 小Hi最近在关注股票,为了计算股票可能的盈利,他获取了一只股票最近N天的价格A1~AN. 小Hi想知道,对于第i天的股票价格Ai,几天之后股价会第一次超过Ai. 假设A=[69, 73, 6 ...
- swift语言点评十三-Lazy
Lazy Stored Properties A lazy stored property is a property whose initial value is not calculated un ...
- [USACO17DEC]Milk Measurement(平衡树)
题意 最初,农夫约翰的每头奶牛每天生产G加仑的牛奶 (1≤G≤109)(1≤G≤10^9)(1≤G≤109) .由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量, ...
- 使用InstelliJ IDEA创建Web应用程序
环境版本 Windows 8.1IDE:InstelliJ IDEA 13 Spring:Spring 4.1.1 & Spring MVC 4.1.1 WebLogic 10.3 ...
- Object-C,NSURL,统一资源定位器
今天晚上最后一个例子,写完休息娱乐一会. URL,统一资源定位器,可以定位网络上的一个资源. 没啥难的,还是对象.方法.API.和Java等语言没有啥区别. 不亲自一点点写一遍,印象不深,今后进一步深 ...
- 题解 洛谷 P3381 【【模板】最小费用最大流】
发了网络流,再来一发费用流 能做费用流的,网络流自然做得来,但在这还是不要脸的安利一下自己的博客(里面也有网络流的题解): 点我 扯远了... 费用流,就是在不炸水管的情况下求源点到汇点的最小费用. ...
- SVN学习总结(2)——SVN冲突解决
在我们用VS进行项目合作开发的过程中,SVN的提交控制是至关重要的,大家不可避免的都遇到过SVN冲突的问题,开发的时候,应该认真学习SVN的知识,减少冲突,集中时间放在开发上. 解决冲突有三种方式: ...