UVa 10152 - ShellSort

找规律。用两个数组分别存放乱序和正序的情况,并记录乱序的编号,讲编号给复给正序的,即第二组样历为 1 6 3 2 4 5 7 8 9 。然后反向遍历正序的数组。当出现 当前位置的序号小于它上一个的序号,则记录,然后从次位置开始遍历输出。


// File Name: UVa10152.cpp
// Author: Toy
// Created Time: 2013年04月17日 星期三 21时29分50秒

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cctype>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <utility>
#include <bitset>
#define L(x) x << 1
#define R(x) x << 1 | 1

using namespace std;

int Case, num, wa_s[205], co_s[205];
string wa[205], co[205];

void init ( ) {
    for ( int i = 1; i <= num; ++i ) 
	for ( int j = 1; j <= num; ++j ) 
	    if ( wa[i] == co[j] ) co_s[j] = wa_s[i];
}

void solve ( ) {
    int tmp;
    for ( int i = num; i >= 2; --i ) 
	if ( co_s[i] < co_s[i - 1] ) {
	    tmp = i - 1;
	    break;
	}
    for ( int i = tmp; i >= 1; --i ) 
	cout << wa[co_s[i]] << endl;
}

int main ( ) {
    cin >> Case;
    while ( Case-- ) {
	cin >> num;
	getchar ( );
	for ( int i = 1; i <= num; ++i ) {
	    getline ( cin, wa[i] );
	    wa_s[i] = i;
	}
	for ( int i = 1; i <= num; getline ( cin, co[i] ), ++i ) ;
	
	init ( );

	solve ( );

	cout << endl;
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值