Created
          January 4, 2011 17:56 
        
      - 
      
 - 
        
Save shnya/765114 to your computer and use it in GitHub Desktop.  
    SRM 344 Div2 Level 2
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class SimpleRotationDecoder | |
| { | |
| vector<string> split(const string &str, const string &delim){ | |
| vector<string> res; | |
| size_t current = 0, found, delimlen = delim.size(); | |
| while((found = str.find(delim, current)) != string::npos){ | |
| res.push_back(string(str, current, found - current)); | |
| current = found + delimlen; | |
| } | |
| res.push_back(string(str, current, str.size() - current)); | |
| return res; | |
| } | |
| vector<string> make_passwords(void){ | |
| vector<string> passwords; | |
| for(size_t i = 0; i < 26; i++){ | |
| for(size_t j = 0; j < 26; j++){ | |
| for(size_t k = 0; k < 26; k++){ | |
| char pass[4]; | |
| pass[0] = i + 'a'; | |
| pass[1] = j + 'a'; | |
| pass[2] = k + 'a'; | |
| pass[3] = '\0'; | |
| passwords.push_back(pass); | |
| } | |
| } | |
| } | |
| return passwords; | |
| } | |
| int _c2i(char c){ | |
| if(c == ' ') | |
| return 0; | |
| else | |
| return c - 'a' + 1; | |
| } | |
| char _i2c(int a){ | |
| if(a == 0) | |
| return ' '; | |
| else | |
| return a - 1 + 'a'; | |
| } | |
| //3 + 26 = 29 % 27 = 2 | |
| //2 - 26 = -24 + 27 = 3 | |
| string _decode(const string &cipher, const string &pass){ | |
| string decode(cipher); | |
| for(size_t i = 0; i < cipher.size(); i++){ | |
| int a = _c2i(cipher[i]) - _c2i(pass[i % 3]); | |
| if(a < 0) a += 27; | |
| decode[i] = _i2c(a); | |
| } | |
| return decode; | |
| } | |
| bool checkRule(string decoded){ | |
| vector<string> words = split(decoded, " "); | |
| for(size_t i = 0; i < words.size(); i++) { | |
| string word = words[i]; | |
| if(word.size() < 2 || word.size() > 8) | |
| return false; | |
| bool flg = false; | |
| for(size_t j = 0; j < word.size(); j++) { | |
| char c = word[j]; | |
| if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') | |
| flg = true; | |
| } | |
| if(!flg) return false; | |
| } | |
| return true; | |
| } | |
| public: | |
| string decode(string cipherText) | |
| { | |
| vector<string> passwords = make_passwords(); | |
| for(size_t i = 0; i < passwords.size(); i++){ | |
| string decoded = _decode(cipherText, passwords[i]); | |
| if(checkRule(decoded)){ | |
| return decoded; | |
| } | |
| } | |
| return string(""); | |
| } | |
| }; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment