zoukankan      html  css  js  c++  java
  • TC SRM 397 2 250

    BreakingTheCode


    Problem Statement

    You have been given a secret mission where you must break the enemy's code. You have already figured out that they encode messages using the following method. Each letter between 'a' and 'z', inclusive, is assigned a distinct two-digit number between 01 and 26, inclusive. A message is encoded by simply replacing each letter with its assigned number. For example, if 't' is assigned 20, 'e' is assigned 05 and 's' is assigned 19, then the message "test" is encoded as "20051920". All original messages contain only lowercase letters.


    You are given a String code containing the assignment of numbers to letters. The first letter of code is assigned 01, the second is assigned 02 and so on. You are also given aString message which is either an original unencoded message or an encoded message. If you are given an unencoded message, return the encoded version of that message, and if you are given an encoded message, return the original unencoded message.

    Definition

    • ClassBreakingTheCode
    • MethoddecodingEncoding
    • Parametersstring , string
    • Returnsstring
    • Method signaturestring decodingEncoding(string code, string message)
    (be sure your method is public)

    Limits

    • Time limit (s)2.000
    • Memory limit (MB)64

    Constraints

    • code will contain exactly 26 characters.
    • Each lowercase letter between 'a' and 'z', inclusive, will occur exactly once in code.
    • message will contain between 1 and 50 characters, inclusive.
    • message will either contain only lowercase letters ('a'-'z') or only digits ('0'-'9').
    • If message contains only digits, it will be a concatenation of two-digit numbers, each between 01 and 26, inclusive.

    Test cases

      1.  
        • code"abcdefghijklmnopqrstuvwxyz"
        • message"test"
         
        Returns"20051920"
         
        Example from the problem statement. Here, the letters are coded in an alphabetical order.
      2.  
        • code"abcdefghijklmnopqrstuvwxyz"
        • message"20051920"
         
        Returns"test"
         
        Now, we're decoding it.
      3.  
        • code"qesdfvujrockgpthzymbnxawli"
        • message"mwiizkelza"
         
        Returns"19242626171202251723"
      4.  
        • code"faxmswrpnqdbygcthuvkojizle"
        • message"02170308060416192402"
         
        Returns"ahxpwmtvza"
          1 #include <cstdio>
          2 #include <cmath>
          3 #include <cstring>
          4 #include <ctime>
          5 #include <iostream>
          6 #include <algorithm>
          7 #include <set>
          8 #include <vector>
          9 #include <sstream>
         10 #include <typeinfo>
         11 #include <fstream>
         12 
         13 using namespace std;
         14 
         15 class BreakingTheCode {
         16     public:
         17     string decodingEncoding(string code, string message) {
         18         int i,j;
         19         int a[30]={0};
         20         string k;
         21         k="";
         22         for(i=0;i<code.size();i++)
         23         {
         24             a[code[i]-'a'+1]=i+1;
         25         }
         26         if('0'<=message[0] && message[0]<='9')
         27         {
         28             for(i=0;i<message.size();i=i+2)
         29             {
         30                 int x=(message[i]-'0')*10;
         31                 x=x+(message[i+1]-'0');
         32                 k=k+code[x-1];
         33             }
         34         }
         35 
         36         else
         37         {
         38             for(i=0;i<message.size();i++)
         39             {
         40                 int x=message[i]-'a'+1,y;
         41                 y=a[x];
         42                 char h;
         43                 h='0'+y/10;
         44                 k=k+h;
         45                 h='0'+y%10;
         46                 k=k+h;
         47             }
         48         }
         49         return k;
         50     }
         51 };
         52 
         53 // CUT begin
         54 ifstream data("BreakingTheCode.sample");
         55 
         56 string next_line() {
         57     string s;
         58     getline(data, s);
         59     return s;
         60 }
         61 
         62 template <typename T> void from_stream(T &t) {
         63     stringstream ss(next_line());
         64     ss >> t;
         65 }
         66 
         67 void from_stream(string &s) {
         68     s = next_line();
         69 }
         70 
         71 template <typename T>
         72 string to_string(T t) {
         73     stringstream s;
         74     s << t;
         75     return s.str();
         76 }
         77 
         78 string to_string(string t) {
         79     return """ + t + """;
         80 }
         81 
         82 bool do_test(string code, string message, string __expected) {
         83     time_t startClock = clock();
         84     BreakingTheCode *instance = new BreakingTheCode();
         85     string __result = instance->decodingEncoding(code, message);
         86     double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
         87     delete instance;
         88 
         89     if (__result == __expected) {
         90         cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
         91         return true;
         92     }
         93     else {
         94         cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
         95         cout << "           Expected: " << to_string(__expected) << endl;
         96         cout << "           Received: " << to_string(__result) << endl;
         97         return false;
         98     }
         99 }
        100 
        101 int run_test(bool mainProcess, const set<int> &case_set, const string command) {
        102     int cases = 0, passed = 0;
        103     while (true) {
        104         if (next_line().find("--") != 0)
        105             break;
        106         string code;
        107         from_stream(code);
        108         string message;
        109         from_stream(message);
        110         next_line();
        111         string __answer;
        112         from_stream(__answer);
        113 
        114         cases++;
        115         if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
        116             continue;
        117 
        118         cout << "  Testcase #" << cases - 1 << " ... ";
        119         if ( do_test(code, message, __answer)) {
        120             passed++;
        121         }
        122     }
        123     if (mainProcess) {
        124         cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
        125         int T = time(NULL) - 1438865246;
        126         double PT = T / 60.0, TT = 75.0;
        127         cout << "Time   : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
        128         cout << "Score  : " << 250 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
        129     }
        130     return 0;
        131 }
        132 
        133 int main(int argc, char *argv[]) {
        134     cout.setf(ios::fixed, ios::floatfield);
        135     cout.precision(2);
        136     set<int> cases;
        137     bool mainProcess = true;
        138     for (int i = 1; i < argc; ++i) {
        139         if ( string(argv[i]) == "-") {
        140             mainProcess = false;
        141         } else {
        142             cases.insert(atoi(argv[i]));
        143         }
        144     }
        145     if (mainProcess) {
        146         cout << "BreakingTheCode (250 Points)" << endl << endl;
        147     }
        148     return run_test(mainProcess, cases, argv[0]);
        149 }
        150 // CUT end
        View Code
  • 相关阅读:
    .NET常见问题收集
    .NET 常用转换
    Android 百度地图开发setOnTouchListener方法给mapView设置了OnTouchListener后地图无法拖动的问题
    Android_参考
    常用操作
    Android开发笔记——Eclipse 关联Android源码
    Android开发笔记静态变量问题
    无法载入 mysql 扩展,<br />请检查 PHP 配置 解决办法
    服务器维护
    cocos2d 左下角三行
  • 原文地址:https://www.cnblogs.com/cyd308/p/4709216.html
Copyright © 2011-2022 走看看