博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2250解题报告
阅读量:4204 次
发布时间:2019-05-26

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

求两个字符串的公共子序列。因为是最基本的算法,所以一次就过了。并且用的是C++的vector和string。。。table数组记录最长公共子序列的长度,如果当前比较的两个字符(本题中为字符串)相当,则table[i][j] = table[i - 1][j - 1] + 1,否则,table[i][j] = max(table[i - 1][j], table[i][j - 1])。table数组的空间肯定可以改进,但想来就挺麻烦的。。。path数组记录路径,0表示当前点包含在最长公共子序列中,1表示向左找,2表示向上找。通过从头往前查path数组就可以构造出最长公共子序列。

注:代码中注释的部分是把一个string按照空格分隔成单词,保存在vector<string>里面,相当于Python或Java里面的split,只是不能指定分隔符。

#include 
#include
#include
#include
#include
using namespace std;string longestcommonsubsequence(const vector
&stra, const vector
&strb){ int **table = new int*[stra.size()]; for(int i = 0; i < stra.size(); ++i) { table[i] = new int[strb.size()]; } int **path = new int*[stra.size()]; for(int i = 0; i < stra.size(); ++i) { path[i] = new int[strb.size()]; } for(int i = 0; i < stra.size(); ++i) { for(int j = 0; j < strb.size(); ++j) { if(stra[i] == strb[j]) { if(i == 0 || j == 0) { table[i][j] = 1; } else { table[i][j] = table[i - 1][j - 1] + 1; } path[i][j] = 0; } else { if(i == 0 && j == 0) { table[i][j] = 0; path[i][j] = 1; } else if(i != 0 && j == 0) { table[i][j] = table[i - 1][j]; path[i][j] = 1; } else if(i == 0 && j != 0) { table[i][j] = table[i][j - 1]; path[i][j] = 2; } else { int len1 = table[i - 1][j]; int len2 = table[i][j - 1]; if(len1 > len2) { table[i][j] = len1; path[i][j] = 1; } else { table[i][j] = len2; path[i][j] = 2; } } } } } string lcs; bool first = true; for(int i = stra.size() - 1, j = strb.size() - 1; i >= 0 && j >= 0;) { if(path[i][j] == 0) { if(first) { lcs.insert(0, stra[i]); first = false; } else lcs.insert(0, stra[i] + " "); i--; j--; } else if(path[i][j] == 1) { i--; } else { j--; } } return lcs;}int main(){ /* string sentence = "die einkommen der landwirte sind fuer die abgeordneten ein buch mit sieben siegeln um dem abzuhelfen muessen dringend alle subventionsgesetze verbessert werden"; istringstream iss(sentence); vector
stra; copy(istream_iterator
(iss), istream_iterator
(), back_inserter
>(stra)); sentence = "die steuern auf vermoegen und einkommen sollten nach meinung der abgeordneten nachdruecklich erhoben werden dazu muessen die kontrollbefugnisse der finanzbehoerden dringend verbessert werden"; //iss.str(sentence); istringstream iss2(sentence); vector
strb; copy(istream_iterator
(iss2), istream_iterator
(), back_inserter
>(strb)); */ vector
stra, strb; string str; while(true) { stra.clear(); strb.clear(); while(true) { cin>>str; if(cin.eof()) return 0; if(str == "#") break; stra.push_back(str); } while(true) { cin>>str; if(str == "#") break; strb.push_back(str); } cout<

转载地址:http://cxxli.baihongyu.com/

你可能感兴趣的文章
Putty远程服务器的SSH经验
查看>>
内核态与用户态
查看>>
使用mingw(fedora)移植virt-viewer
查看>>
趣链 BitXHub跨链平台 (4)跨链网关“初介绍”
查看>>
C++ 字符串string操作
查看>>
MySQL必知必会 -- 了解SQL和MySQL
查看>>
MySQL必知必会 -- 使用MySQL
查看>>
MySQL必知必会 -- 数据检索
查看>>
MySQL必知必会 -- 排序检索数据 ORDER BY
查看>>
MySQL必知必会 -- 数据过滤
查看>>
MYSQL必知必会 -- 用通配符进行过滤
查看>>
MYSQL必知必会 -- 用正则表达式进行搜索
查看>>
MySQL必知必会 -- 创建计算字段
查看>>
MySQL必知必会 -- 使用数据处理函数
查看>>
MySQL必知必会 -- 数据汇总
查看>>
MySQL必知必会 -- 子查询的使用
查看>>
POJ 3087 解题报告
查看>>
POJ 2536 解题报告
查看>>
POJ 1154 解题报告
查看>>
POJ 1661 解题报告
查看>>