Morgan Stanley Programming Constest 2014题目Train
来源:互联网 发布:王兀 中国好歌曲 知乎 编辑:程序博客网 时间:2024/05/22 01:35
思路
本题中的“操作”根据题干描述并不是很清楚,但根据示例数据可以推断出实际是求一共要卸掉多少车皮和新装上多少车皮。并且根据题意容易推断出卸掉的车皮数是进站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度,新装节数是出站火车的节数减去进站火车和出站火车的最长公共车皮序列的长度。从而本题的关键是求进站车和出站车的最大公共子序列的长度。
代码
#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;}
0 0
- Morgan Stanley Programming Constest 2014题目Train
- Morgan Stanley Programming Constest 2014题目Free Fall
- Morgan Stanley Program Contest 2014-Train
- 【2014校招】Morgan Stanley - phone interview
- morgan stanley IT interview
- 应聘Morgan Stanley(转)
- Morgan Stanley OA题库
- 【Morgan Stanley】AC面试
- morgan stanley 电话面试
- morgan stanley 面试资料(一)
- morgan stanley 面试资料(二)
- 我对morgan stanley it的了解
- 【Morgan Stanley IKM在线测试】C++
- 【面试】Morgan Stanley IT面经
- Morgan Stanley IT笔试面试经验
- 【Morgan Stanley IT暑期实习生电面】
- Morgan Stanley(摩根士丹利)电面过程
- Morgan Stanley(摩根士丹利)电面过程
- Statement 与PreparedStatement的区别
- HDU 2574 Hdu Girls' Day
- 关于C++里面使用set_union,set_intersection等函数的使用总结
- 用Maven创建第一个web项目
- 在Ajax中使用XML通信
- Morgan Stanley Programming Constest 2014题目Train
- 文件复制功能
- 使用ASM动态生成class文件
- SQL Server中批处理的知识点
- linux删除多级非空目录
- 一些好的博客
- Java HashMap遍历
- 队列逆置
- DevExpress XtraTreeList 节点拖拽