uva10152 ShellSort
题意:输入2个乌龟名字的序列, 把第一个输入序列变换为第二个序列
变换的规则是乌龟只能从底往上爬且只能爬到顶不能插入中间。
解法:把2个序列自下而上找匹配,若匹配,这两个序列同时往上找下一个名字,若不匹配则原序列往上找一个名字,直到原序列找到顶部,看看需变换的序列还有几个名字,把这几个名字自下而上输出即可。
代码:
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <algorithm>
int t, n;
char star[205][105];
char end[205][105];
int main()
{
cin >> t;
while(t --)
{
cin >> n;
getchar();
for (int i = 1; i <= n; i ++)
gets(star[i]);
for (int i = 1; i <= n; i ++)
gets(end[i]);
int i = n;
int j = n;
while (i != 0)
{
if(strcmp(star[i],end[j]) == 0)
{
i --;
j --;
}
else
{
i --;
}
}
for (int k = j - i; k > 0; k --)
cout << end[k] << endl;
cout << endl;
}
return 0;
}