In many computer problems, it is necessary to permute data arrays. That is, the data in an array mustbe re-arranged in some specified order. One way to permute arbitrary data arrays is to specify thepermutations with an index array to point out the position of the elements in the new array. Let xbe an array that is to be permuted and let x′ be the permuted array. Then, we have the relationshipbetween x and x′ that x′pi = xi.
Input
The input begins with a single positive integer on a line by itself indicating the number of the casesfollowing, each of them as described below. This line is followed by a blank line, and there is also ablank line between two consecutive inputs.
Each input set will contain two lines of numbers. The first line will be an index array p containingthe integers 1 . . . n, where n is the number of integers in the list. The numbers in the first line will havebeen permuted in some fashion. The second line will contain a list numbers in floating point format.
Output
For each test case, the output must follow the description below. The outputs of two consecutive caseswill be separated by a blank line.
The output for this program will be the list of floating point numbers from the input set, orderedaccording to the permutation array from the input file. The output numbers must be printed one perline in the same format in which they each appeared in the input file.
Sample Input
1
3 1 2
32.0 54.7 -2
Sample Output
54.7
-2
32.0
问题链接:UVA482 Permutation Arrays
问题简述:
两行输入分两组数据,第一行是编号,第二行是数据值。要求按照第一行编号顺序输出第二行的值。
问题分析:第一行的编号可以看作是一种置换,指定第二行数据值的输出顺序。把编号当作一种下标索引即可。
这个题坑爹的地方是没有指出数据的数量,只好设N的值为10000。
程序说明:
每行的数据个数也是不知道的,所以需要用点程序技巧,读到换行符则结束编号的输入。
第二行浮点数的处理是比较麻烦的,按浮点数读入的话,就难以与原始数据相同的数字输出了,只能按字符串读入数据比较理想。
题记:(略)
参考链接:(略)
/* UVA482 Permutation Arrays */
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
const int N = 1000;
string s[N + 1];
int is[N + 1];
int main()
{
int t, bl = 0;
cin >> t;
while(t--) {
int a, i, j;
for(i=1; ;i++) {
cin >> a;
is[a] = i;
if(getchar() == '\n')
break;
}
for(j=1; j<=i; j++)
cin >> s[j];
if(bl++)
cout << endl;
for(i=1; i<j; i++)
cout << s[is[i]] << endl;
}
return 0;
}