Morgan Stanley Program Contest 2014-Train

You work at a train station that’s responsible for rearranging the carriages of long vehicles on-the-fly. This is a very complicated work, as carriages have to be attached or removed one-by-one using a single fork.



Your job is to calculate the least number ofoperations you have to perform to have the train rearranged.
 
Input format

You are provided two strings. The first onelists the carriages of the incoming train from head to tail. The second showshow they should look like after leaving the station. Each cargo type isrepresented by a lowercase letter of the English alphabet. You can assume thatyou have unlimited amount of carriages to attach and unlimited space to storethe removed ones. Incoming and outgoing trains never consist of more than 34000carriages, but they are allowed to grow longer during the rearrangementprocess.

Output format

You need to specify a single integer that isequal to the least number of attach and remove operations required to rearrangethe train.

Sample input

abacdezx
bascdet

Sample output

5


my solution:

#include <stdio.h>
#define MIN(a,b,c) (a>b?(b>c?c:b):(a>c?c:a))
#define DIFF(t1,t2) (t1==t2?0:1)
int ver[34001] = {0};
char str1[34001];
char str2[34001];
void main()
{
        char c;
        FILE *fp = fopen("input20.txt", "rw+");
        FILE *fp2 = fopen("output20.txt", "w+");
        bool isfirst = true;
        while(fscanf(fp, "%s %s %c", str1, str2, &c) != EOF) {
        //fscanf(fp, "%s %s %c", str1, str2, &c);
                int i = 0;
                int j = 0;
                ver[0] = 0;
                int tmp;
                int hortmp = 0;
                while(str2[j] != '\0') {
                        ver[j+1] = j + 1;
                        j++;
                }
                while(str1[i] != '\0') {
                        hortmp = i + 1;
                        j = 0;
                        tmp = 0;
                        while(str2[j] != '\0') {
                                tmp = hortmp;
                                hortmp = MIN(ver[j]+1, ver[j+1]+1, DIFF(str1[i], str2[j])+ver[j]);
                                ver[j] = tmp;
                                j++;
                        }
                        ver[j] = hortmp;
                        i++;
                }
                printf("%d\n", ver[j]);
                if(isfirst)
                        fprintf(fp2, "%d\n",ver[j]);
                else
                        fprintf(fp2, ";\n%d\n", ver[j]);
                isfirst = false;
        }
}


data sample in input20.txt:

xlgschsfvqmnhxkfiniysacnfhtdoldiiquwnfesuxidjtkdjrhxbjyrvrrzusfhwukjaqcwygmv
olgcxthsncvcqmngxfinaacnhhctdoldiquwesucilsdjtkedrxvsbyrvmrztfiuukacgkwgm
;
jiahhywmkoxgqrmzjzmzqpmhkdhhanemnyhwazozlzndbardzopejiwxuvpurwswokiveczncnkosrtjrvasebfyksuhcojmmfoqszukdrddbnjgzqwngkxwprwxntnijsrekvgfnhivqqypruvysbctgqzyyhlwjznzolmcmpltsvajmealkqsemovqqmqoduhplpwoqdeegrnnqalfsbqbbeorkguuhwumecagnseeafbgabwuvwxqrrstftmmsdpthqmxljdabdgkjhogcncgffkecleehihxxyndgqaqqmnpqwqzwzemtkhhsqzkbojmdpguosrwqtvgpenqmssyqjmwjmenaamjivwdojhovburibzjzfxlabdvsdyffavwqlvyyjclokmhtekeutlxknvqxmnqegjyvcakescjofcxxxzmycepvzxitafidjrbozrtasocuifoflrtbbjjpyilgihhtdwugrhefhpgspkwtntcqrcgnpziqgvzqnatpncmewjsgungylhhgsnvemliforbvpyggbsprapgsvahbjleswqboqmbrcqhavwazmyhvenxhhwpxlifyyagcasqmzyiilfjftnwmmnulklvnnflzbauthwwmrawgmowocproiryzimriedlclurjzafahtjhyjbhjswuodtkcqfeqyadeffokglxboglogvattyghysdwojqjeq
jhhywxmkfxgpqrzbjzqmhkqhbmenyczczbwrjgdzoilueiwxqtvpuurwsoijecjzsnlkywrtjfrzvaexjyeeksouhfaeccohpmmqszqkdcdddlpijgqwhgxjwnpruwxzntnijsxekgfyngvlqyifltauwysbcqxiwyhhaywwzmnjzohammpltqsvjmeiaiqschmvqwlqodhplcpnlwodaeggrnnqclbbqpjbrbheorunuhkqueaynseafgiawujmvxxqrrnmsttisbmshthuqmpxldhinasbdgijhjogcgezfzfksfcleixwnjxghqaqmiiwqweomkhsnkbfoxmypgslwqtnvpbwqwmssmvjmmendfqapmjigvwdyozbjriqbsozxlabydmvyflsdfnavwqxvyyjclokghhegketblknvqxxmnqmgoyvcaugikoqesjcejjwofxpxzyceyznxhitajdbjozctycscuioltbbtjjiiiohrwuipwrurdfyxlnlhepkwncvqcinpfyzqgnovrziqspvqxwojuisunghpggngqmiforwbvynvgkycypsrlapgsahbvjlteswvotqbonqfqyrcsqlavrazpmzhrehwlxbfyagcasizyilfjfngwmmldkvnfzbasuahwthwwmzramowohdcporqtrzdimdebylunrjzahxtjqybqswuojtmqkclvfeqwyaefjfokglxbkqowgvatnyhysdwjeq


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值