小王的保险箱密码是一个升序排列的数字串,但是小王总是记不住他的密码,于是小王将他的密码加密后保存在了一个文本文件里,加密的流程如下:
1.用数字的英文单词来代替数字本身,比如134699变成onethreefoursixninenine
2.将上述字符串使用小王加密算法进行处理。该算法会按照某种规则来改变字符串字符的排列顺序,同时还会改变某些字母的大小写,比如onethreefoursixninenine经过加密后就变成了NeNohuiroNNiNeteefersix
由于小王加密算法是小王自己设计的,所以小王认为只有他自己能将加密后的字符串还原。
实际上小王的加密算法存在漏洞,即使不知道小王加密算法的具体实现细节,也是可以还原出原始的密码的。请你写一段程序来破解小王的密码。
输入描述:
一段英文非空字符串,且字符串可以完美还原出数字。
输出描述:
请输出原始的密码
示例1:
输入
oNEthrEEfoursixNiNENiEN
输出:
134699
————————————————
#include <iostream>
#include <vector>
#include <cctype>
#include <string>
using namespace std;
//vector<int> charEycpt(string str)
vector<int> charEycpt(string str)
{
string::size_type j = 0;
vector<int> cnt(255, 0);
vector<int> num(10, 0);
//string res = "";
for (string::size_type ix = 0; ix != str.size(); ++ix)
{
if (isupper(str[ix]))
str[ix] = tolower(str[ix]);
cnt[str[ix]]++;
}
//0
while (cnt['z'])
{
cnt['z']--;
cnt['e']--;
cnt['r']--;
cnt['o']--;
//res += '0';
//num[0]++;
}
//2
while (cnt['w'])
{
cnt['t']--;
cnt['w']--;
cnt['o']--;
//res += '2';
num[2]++;
}
//8
while (cnt['g'])
{
cnt['e']--;
cnt['i']--;
cnt['g']--;
cnt['h']--;
cnt['t']--;
//res += '8';
num[8]++;
}
//6
while (cnt['x'])
{
if (cnt['s'] && cnt['i'])
{
cnt['s']--;
cnt['i']--;
cnt['x']--;
//res += '6';
num[6]++;
}
//else
// break;
}
//7
while (cnt['s'])
{
if (cnt['v'] && cnt['n'] && (cnt['e'] > 1))
{
cnt['s']--;
cnt['e'] -= 2;
cnt['v']--;
cnt['n']--;
//res += '7';
num[7]++;
}
//else
// break;
}
//3
while (cnt['h'])
{
if (cnt['t'] && cnt['r'] && (cnt['e'] > 1))
{
cnt['t']--;
cnt['h']--;
cnt['r']--;
cnt['e'] -= 2;
//res += '3';
num[3]++;
}
//else
// break;
}
//1
while (cnt['f'])
{
if (cnt['f'] && cnt['u'] && cnt['o'] && cnt['r'])
{
cnt['f']--;
cnt['o']--;
cnt['u']--;
cnt['r']--;
//res += '4';
num[4]++;
}
else
if (cnt['f'] && cnt['i'] && cnt['v'] && cnt['e'])
{
cnt['f']--;
cnt['i']--;
cnt['v']--;
cnt['e']--;
//res += '5';
num[5]++;
}
else
break;
}
//1
while (cnt['o'])
{
if (cnt['n'] && cnt['e'])
{
cnt['o']--;
cnt['n']--;
cnt['e']--;
//res += '1';
num[1]++;
}
//else
// break;
}
//9
while (cnt['i'])
{
cnt['i']--;
//res += '9';
num[9]++;
}
return num;
}
int main()
{
//string str_test = "oNEthrEEfoursixNiNENiNE";
string str_test;
cin >> str_test;
vector<int> re;
re = charEycpt(str_test);
for (vector<int>::size_type ix = 0; ix != re.size(); ++ix)
while(re[ix]--)
cout<<ix;
cout << endl;
return 0;
}
结果测试: