Morgan Stanley Programming Constest 2014题目Train

思路

本题中的“操作”根据题干描述并不是很清楚,但根据示例数据可以推断出实际是求一共要卸掉多少车皮和新装上多少车皮。并且根据题意容易推断出卸掉的车皮数是进站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度,新装节数是出站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度。从而本题的关键是求进站车和出站车的最大公共子序列的长度。

代码

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值