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


發佈了36 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章