• ACM/ICPC ZOJ1009-Enigma 解题代码


    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        int strwide;
        cin >> strwide;
        pair< char, char > rotor[3][1000];
        pair< char, char > rotor_instead[3][1000];
        string input;
        int t = 0;
    
        
        while( t < 3 )
        {
            cin >> input;
            if ( input.size() == strwide )
            {
                char start_1 = 'a';
                char start_else = 'A';
                if ( t == 0 )
                {
                    for ( int m = 0; m < input.size(); m++ )
                    {
                        rotor[t][m].first = start_1;
                        rotor[t][m].second = input[m];
                        start_1 = start_1 + 1;
                    }
                }
                else
                {
                    for ( int m = 0; m < input.size(); m++ )
                    {
                        rotor[t][m].first = start_else;
                        rotor[t][m].second = input[m];
                        start_else = start_else + 1;
                    }
                }
                //rotor[t] = input;
                t++;
            }
        }
    
        int crypto_number;
        cin >> crypto_number;
        //string *crypto = new string[crypto_number];
        string crypto[1000];
        t = 0;
        string input_crypto;
        while( cin >> input_crypto && input_crypto != "0" && t < crypto_number )
        {
            crypto[t] = input_crypto;
            t++;
        }
        /*for ( int l = 0; l < t; l++ )
        {
            cout << crypto[l]<< endl;
        }*/
        //string *crypto_return = new string[crypto_number];
        for ( int i = 0; i < crypto_number; i++ )
        {
            for( int a = 0; a < 3; a++ )
            {
                for ( int b = 0; b < strwide; b++ )
                {
                    rotor_instead[a][b] = rotor[a][b];
                }
            }
            //int second_rotor_count = 0;
            //char *crypto_return = new char[crypto[i].size()];
            char crypto_return[1000];
            //cout << crypto[i].size() << endl;
            for ( int j = 0; j < crypto[i].size(); j++ )
            {
                for ( int r = 0; r < strwide; r++ )
                {
                    if ( rotor_instead[2][r].second == crypto[i][j])
                    {
                        crypto_return[j] = rotor_instead[2][r].first;
    
                        for ( int k = 0; k < strwide; k++ )
                        {
                            if ( rotor_instead[1][k].second == crypto_return[j])
                            {
                                crypto_return[j] = rotor_instead[1][k].first;
                                for ( int s = 0; s < strwide; s++ )
                                {
                                    if ( rotor_instead[0][s].second == crypto_return[j] )
                                    {
                                        crypto_return[j] = rotor_instead[0][s].first;
                                    }
                                }
                            }
                            
                        }
                    }
                    
                }
    
                for ( int d = 0; d < strwide; d++ )
                {
                    rotor_instead[0][d].first += 1;
                    rotor_instead[0][d].second += 1;
                    if (rotor_instead[0][d].first == 'a'+ strwide )
                    {
                        rotor_instead[0][d].first -= strwide;
                    }
                    if ( rotor_instead[0][d].second == 'A'+strwide )
                    {
                        rotor_instead[0][d].second -= strwide;
                    }
                }
    
                if ( (j+1)%strwide == 0 )
                {
                    //second_rotor_count++;
                    for ( int d = 0; d < strwide; d++ )
                    {
                        rotor_instead[1][d].first += 1;
                        rotor_instead[1][d].second += 1;
                        if ( rotor_instead[1][d].first == 'A'+strwide )
                        {
                            rotor_instead[1][d].first -= strwide;
                        }
                        if ( rotor_instead[1][d].second == 'A'+strwide )
                        {
                            rotor_instead[1][d].second -= strwide;
                        }
                    }
                }
    
                if ( (j + 1)%( strwide*strwide) == 0 )
                {
                    for ( int d = 0; d < strwide; d++ )
                    {
                        rotor_instead[2][d].first += 1;
                        rotor_instead[2][d].second += 1;
                        if ( rotor_instead[2][d].first == 'A'+strwide )
                        {
                            rotor_instead[2][d].first -= strwide;
                        }
                        if ( rotor_instead[2][d].second == 'A'+strwide )
                        {
                            rotor_instead[2][d].second -= strwide;
                        }
                    }
                }
    
            }
    
            /*for ( int a = 0; a < 3; a++ )
            {
                for ( int b = 0; b < strwide; b++ )
                {
                    cout << rotor_instead[a][b].first <<" "<< rotor_instead[a][b].second << endl;
                }
            }*/
            cout << "Enigma "<< i+1 <<":"<< endl;
            for ( int c = 0; c < crypto[i].size(); c++ )
            {
                cout << crypto_return[c];
            }
            cout << endl;
            if ( i != crypto_number - 1 )
            {
                cout << endl;
            }
    
        }
    
        return 0;
    }
  • 相关阅读:
    selenium--下拉列表选择
    Java——基本类型包装类,System类,Math类,Arrays类,BigInteger类,BigDecimal类
    Java——Object类,String类,StringBuffer类
    Java——面向对象进阶(final关键字,static关键字,匿名对象,内部类,四种访问修饰符,代码块)
    Java——面向对象进阶(构造方法,this,super)
    Java——面向对象进阶(抽象类,接口)
    Java——面向对象编程
    java——类型转换,冒泡排序,选择排序,二分查找,数组的翻转
    CentOS7下安装MySQL
    Java——定义类,引用类数据类型,集合类型(array list)
  • 原文地址:https://www.cnblogs.com/2011winseu/p/3178636.html
Copyright © 2020-2023  润新知