首页 > 其他 > 详细

Morgan Stanley Program Contest 2014-Train

时间:2014-03-03 12:48:00      阅读:440      评论:0      收藏:0      [点我收藏+]
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.

bubuko.com,布布扣

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];
int 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;
        }
}




Morgan Stanley Program Contest 2014-Train,布布扣,bubuko.com

Morgan Stanley Program Contest 2014-Train

原文:http://blog.csdn.net/acmant/article/details/20286419

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!