标签:file lcs chrono int 公共 str time 序列 最长
#include<bits/stdc++.h>
#include <chrono>
using namespace std;
int N;
string X ;
string Y ;
vector<vector<int>> c; //动态规划表
set<string> lcs;
vector<double> time_list(5);
int lcs_length(int m, int n){
c = vector<vector<int> >(m+1,vector<int>(n+1));
for(int i=0; i<m+1; ++i){
for(int j=0; j<n+1; ++j){
if (i == 0 || j == 0)
c[i][j] = 0;
else if(X[i-1] == Y[j-1])
c[i][j] = c[i-1][j-1] + 1;
else
c[i][j] = max(c[i-1][j], c[i][j-1]);
}
}
return c[m][n];
}
void lcs_print(int i, int j, string lcs_str){
while (i>0 && j>0){
if (X[i-1] == Y[j-1]){
lcs_str.push_back(X[i-1]);
--i;
--j;
}
else{
if (c[i-1][j] > c[i][j-1])
--i;
else if (c[i-1][j] < c[i][j-1])
--j;
else{
lcs_print(i-1, j, lcs_str);
lcs_print(i, j-1, lcs_str);
return;
}
}
}
reverse(lcs_str.begin(),lcs_str.end());
lcs.insert(lcs_str);
}
int main(){
string from = "C:\\Users\\ASUS\\Desktop\\AL\\ex2\\input\\1_2_input.txt";
string time = "C:\\Users\\ASUS\\Desktop\\AL\\ex2\\output\\time.txt";
ifstream file_in;
file_in.open(from);
ofstream time_out;
time_out.open(time);
for(int i = 0; i < 5; i++){
file_in>>N;
file_in>>X>>Y;
int m = X.length();
int n = Y.length();
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
int length = lcs_length(m, n);
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
cout << "The length of LCS is " << length << endl;
string str;
lcs_print(m, n, str);
ofstream file_out;
file_out.open("C:\\Users\\ASUS\\Desktop\\AL\\ex2\\output\\result_" + to_string(N) + ".txt");
file_out << "The number of LCSs is " << lcs.size() << endl;
set<string>::iterator it = lcs.begin();
for( ; it!=lcs.end(); it++)
file_out << *it << endl;
lcs.clear();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
time_list[i] = time_span.count();
}
for(int i = 0; i < 5; i++)
time_out << time_list[i] << "s" << endl;
return 0;
}
标签:file,lcs,chrono,int,公共,str,time,序列,最长 来源: https://www.cnblogs.com/ayanyuki/p/15652073.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。