博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2250
阅读量:6852 次
发布时间:2019-06-26

本文共 2098 字,大约阅读时间需要 6 分钟。

dp,最长公共子序列变形

View Code
#include 
#include
#include
#include
using namespace std;const int maxn = 105, maxl = 45;int text1[maxn], text2[maxn], len1, len2;char dic[maxn * 2][maxl];int tot;char first[maxl];int f[maxn][maxn], from[maxn][maxn];int stk[maxn];int getid(char *word){ for (int i = 0; i < tot; i++) if (strcmp(dic[i], word) == 0) return i; strcpy(dic[tot++], word); return tot - 1;}void input(int text[], int &len){ char word[maxl]; int i = 1; strcpy(word, first); while (strcmp(word, "#")) { text[i++] = getid(word);// printf("%d %d\n", i - 1, text[i - 1]); scanf("%s", word); } len = i - 1;}void work(){ for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (f[i - 1][j] > f[i][j - 1]) { from[i][j] = 0; f[i][j] = f[i - 1][j]; } else { from[i][j] = 1; f[i][j] = f[i][j - 1]; } if (text1[i] == text2[j] && f[i][j] < f[i - 1][j - 1] + 1) { f[i][j] = f[i - 1][j - 1] + 1; from[i][j] = 2; }// printf("%d %d %d\n", i, j, f[i][j]); } } int a = len1, b = len2; int rear = 0; while (a && b) { if (from[a][b] == 0) a--; else if (from[a][b] == 1) b--; else { stk[rear++] = text1[a]; a--; b--; } }// printf("%d %d %d\n", rear, len1, len2); printf("%s", dic[stk[rear - 1]]); for (int i = rear - 2; i >= 0; i--) printf(" %s", dic[stk[i]]); printf("\n");}int main(){ //freopen("t.txt", "r", stdin); while (~scanf("%s", first)) { memset(f, 0, sizeof(f)); tot = 0; input(text1, len1); scanf("%s", first); input(text2, len2); work(); } return 0;}

转载于:https://www.cnblogs.com/rainydays/archive/2012/07/02/2573055.html

你可能感兴趣的文章
Looking for APAC Operations IT XML Database Developer in Shenzhen and Hongkong
查看>>
MySQL Account Name
查看>>
Redis实现锁
查看>>
docker-swarm集群创建
查看>>
vmware 12 许可证秘钥
查看>>
Maven学习总结(五)——聚合与继承
查看>>
Myeclipse常用快捷键
查看>>
我的友情链接
查看>>
RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
查看>>
Maven学习总结(一)——Maven入门
查看>>
php+html5实现无刷新图片上传
查看>>
STL: 自定义Allocator.
查看>>
几行代码为自己的网站添加划词翻译功能
查看>>
我的友情链接
查看>>
深入理解gradle编译-语法篇
查看>>
Linux服务器管理Shell经典命令
查看>>
入职三天,公司给了100块钱叫我走人
查看>>
git revert和git reset的区别
查看>>
SQL调优:带函数的谓词导致CBO Cardinality计算误差
查看>>
Java语言中参数值传递和引用传递比较
查看>>