Saturday, July 19, 2014

UVa - 11385 - Da Vinci Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#define M 1000000
using namespace std;
int main(){
    vector <int> fib, numb, container;
    fib.push_back(1);
    fib.push_back(2);
    for(int i = 2; i < 45; i++)fib.push_back((fib[i-2]+fib[i-1]));
    char cipher[150];
    int t, n, num;
    cin >> t;
    while(t--){
        cin >> n;
        for(int i = 0; i < n; i++){
            cin >> num; numb.push_back(num);
        }
        getchar();
        gets(cipher);
        int z = 0;
        int l = strlen(cipher);
        char temp[l];
        for(int i = 0; i < l; i++)
            if(cipher[i] >= 'A' && cipher[i] <= 'Z'){
                temp[z] = cipher[i];
                z++;
            }
        temp[z] = '\0';
        for(int i = 0; i < n; i++)
            for(int j = 0; j < 45; j++)
                if(fib[j] == numb[i])container.push_back(j);
        int mx = 0;
        for(int i = 0; i < n; i++)
            if(mx < container[i])mx = max(mx, container[i]);
        char decipher[mx+1];
        memset(decipher, ' ', sizeof(decipher));
        int sz = container.size();
        for(int i = 0; i < sz; i++)decipher[container[i]] = temp[i];
        decipher[mx+1] = '\0';
        puts(decipher);
        container.clear();numb.clear();
        memset(cipher, 0, sizeof(cipher));
        memset(temp, 0, sizeof(temp));
    }
    return 0;
}

2 comments: