Problem A. Speaking in Tongues

字母替换

// Test.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <map>

using namespace std;


int main()
{
    map<char, char> chatMap;
    chatMap.insert(make_pair('a', 'y'));
    chatMap.insert(make_pair('b', 'h'));
    chatMap.insert(make_pair('c', 'e'));
    chatMap.insert(make_pair('d', 's'));
    chatMap.insert(make_pair('e', 'o'));
    chatMap.insert(make_pair('f', 'c'));
    chatMap.insert(make_pair('g', 'v'));
    chatMap.insert(make_pair('h', 'x'));
    chatMap.insert(make_pair('i', 'd'));
    chatMap.insert(make_pair('j', 'u'));
    chatMap.insert(make_pair('k', 'i'));
    chatMap.insert(make_pair('l', 'g'));
    chatMap.insert(make_pair('m', 'l'));
    chatMap.insert(make_pair('n', 'b'));
    chatMap.insert(make_pair('o', 'k'));
    chatMap.insert(make_pair('p', 'r'));
    chatMap.insert(make_pair('q', 'z'));
    chatMap.insert(make_pair('r', 't'));
    chatMap.insert(make_pair('s', 'n'));
    chatMap.insert(make_pair('t', 'w'));
    chatMap.insert(make_pair('u', 'j'));
    chatMap.insert(make_pair('v', 'p'));
    chatMap.insert(make_pair('w', 'f'));
    chatMap.insert(make_pair('x', 'm'));
    chatMap.insert(make_pair('y', 'a'));
    chatMap.insert(make_pair('z', 'q'));

    FILE* pf = fopen("out.txt", "w+");
    int caseNum = 0;
    scanf("%d\n", &caseNum);

    char* s = (char*)malloc(10000 * sizeof(char));
    string* t = new string[caseNum];
    memset(s, 0, 10000 * sizeof(char));
    for(int i = 0; i<caseNum; ++i)
    {
        memset(s, 0, 10000 * sizeof(char));
        t[i] = "";
        gets(s);
        int len = strlen(s);
        
        map<char, char>::iterator iter;
        for(int j = 0; j<len; ++j)
        {
            if(s[j] == ' ')
            {
                t[i].push_back(' ');
                continue;
            }
            iter = chatMap.find(s[j]);
            if(iter != chatMap.end())
                t[i].push_back(iter->second);
        }
        
        
    }

    for(int k = 0; k<caseNum; ++k)
    {
        memset(s, 0, 10000 * sizeof(char));
        sprintf(s, "Case #%d: %s\n", k+1, t[k].c_str());
        fputs(s, pf);
    }

    free(s);
    delete [] t;

    return 0;
}
原文地址:https://www.cnblogs.com/dangerman/p/2959504.html