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