思路
本题中的“操作”根据题干描述并不是很清楚,但根据示例数据可以推断出实际是求一共要卸掉多少车皮和新装上多少车皮。并且根据题意容易推断出卸掉的车皮数是进站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度,新装节数是出站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度。从而本题的关键是求进站车和出站车的最大公共子序列的长度。
代码
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <ctime>
#include <memory.h>
long LCS(std::string& str1, std::string& str2)
{
int length1 = str1.length() + 1;
int length2 = str2.length() + 1;
int line1[35000];
int line2[35000];
int *first, *second;
memset(line1, 0, length1*sizeof(int));
for(int i = 1; i < length1; i++)
{
for(int j = 1; j < length2; j++)
{
first = i % 2 == 1 ? line1 : line2;
second = i % 2 == 1 ? line2 : line1;
second[0] = 0;
if(*(str1.c_str()+i-1) == *(str2.c_str()+j-1))
second[j] = first[j-1] + 1;
else
second[j] = std::max(second[j-1], first[j]);
}
}
second = length1 % 2 == 0 ? line2 : line1;
int lcs_length = second[length2-1];
return lcs_length;
}
int main()
{
time_t start = clock();
std::ifstream fin("input20.txt");
std::ofstream fout("output.txt");
if(fin.fail() || fout.fail())
exit(1);
while(true)
{
std::string incoming_train;
std::string leaving_train;
fin >> incoming_train;
fin >> leaving_train;
int result = LCS(incoming_train, leaving_train);
result = incoming_train.length() + leaving_train.length() - 2 * result;
fout << result << std::endl;
fout.flush();
std::cout << "One Case finished" << std::endl;
while(!fin.eof() && !isalnum(fin.peek()))
fin.get();
if(!fin.eof())
fout << ';' << std::endl;
else
break;
}
fin.close();
fout.close();
std::cout << clock() - start << std::endl;
return 0;
}