一种简单的解密算法

问题:

假设你为自己的账户设置六位数字密码,且数字按顺序排列,首先将每个数字都换成英文单词,如123456变成onetwothreefourfivesix, 然后对字符串任意打乱顺序,或改大小写,如Xisevifruofeethtowteno等,最后需要根据这个任意字符串推算出你最初设置的六位数字密码?

思路

  1. 观察one,two,three,four,five,six,seven,eight,nine九个英文单词中只有two里面包含’w’,four里面包含’u’,six里面包含’x’,eight里面包含’g’,所以首先依次判断字符串中是否包含’w’, ‘u’,‘x’,'g’四个字符;
  2. 若包含,则必定包含它所对应的英文单词,将字符串中该单词中的字符全部替换成1;
  3. 再观察,去掉偶数但此后,若字符串中还包含’o’,那么肯定包含’one’,若包含,则同样替换成1,;
  4. 以此类推,剩下的字符若包含’t’,则存在"three",若包含’f’,则存在"five",若包含’s’,则存在"seven", 若包含’n’,则存在"nine"
  5. 注意,每个单词可能存在不止一个,所以查找时需要循环遍历

代码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

bool ReplaceWord(const char c, std::string& str)
{
	int i1, i2,i3,i4,i5;
	switch(c)
	{
	case 'w':
		i1 = str.find('t');
		str[i1] = '1';
		i2 = str.find('w');
		str[i2] = '1';
		i3 = str.find('o');
		str[i3] = '1';
		break;
	case 'u':
		i1 = str.find('f');
		str[i1] = '1';
		i2 = str.find('o');
		str[i2] = '1';
		i3 = str.find('u');
		str[i3] = '1';
		i4 = str.find('r');
		str[i4] = '1';
		break;
	case 'x':
		i1 = str.find('s');
		str[i1] = '1';
		i2 = str.find('i');
		str[i2] = '1';
		i3 = str.find('x');
		str[i3] = '1';
		break;
	case 'g':
		i1 = str.find('e');
		str[i1] = '1';
		i2 = str.find('i');
		str[i2] = '1';
		i3 = str.find('g');
		str[i3] = '1';
		i4 = str.find('h');
		str[i4] = '1';
		i5 = str.find('t');
		str[i5] = '1';
		break;
	case 'o':
		i1 = str.find('o');
		str[i1] = '1';
		i2 = str.find('n');
		str[i2] = '1';
		i3 = str.find('1');
		str[i3] = '1';
		break;
	case 'r':
		i1 = str.find('t');
		str[i1] = '1';
		i2 = str.find('h');
		str[i2] = '1';
		i3 = str.find('r');
		str[i3] = '1';
		i4 = str.find('e');
		str[i4] = '1';
		i5 = str.find('e');
		str[i5] = '1';
		break;
	case 's':
		i1 = str.find('s');
		str[i1] = '1';
		i2 = str.find('e');
		str[i2] = '1';
		i3 = str.find('v');
		str[i3] = '1';
		i4 = str.find('e');
		str[i4] = '1';
		i5 = str.find('n');
		str[i5] = '1';
		break;
	case 'f':
		i1 = str.find('f');
		str[i1] = '1';
		i2 = str.find('i');
		str[i2] = '1';
		i3 = str.find('v');
		str[i3] = '1';
		i4 = str.find('e');
		str[i4] = '1';
		break;
	case 'i':
		i1 = str.find('n');
		str[i1] = '1';
		i2 = str.find('i');
		str[i2] = '1';
		i3 = str.find('n');
		str[i3] = '1';
		i4 = str.find('e');
		str[i4] = '1';
		break;
	default:
		break;
	}

	return true;
}


bool ContainSpecialNum(std::string& str, std::vector<int>& vecInt, const char c)
{
	for(int i = 0; i <str.length(); i++)
	{
		if(str[i] == c)
		{
			switch(c)
			{
			case 'w':
				vecInt.push_back(2);
				//删除一个two单词
				ReplaceWord('w', str);

				break;
			case 'u':
				vecInt.push_back(4);
				ReplaceWord('u', str);
				break;

			case 'x':
				vecInt.push_back(6);
				ReplaceWord('x', str);
				break;
			case 'g':
				vecInt.push_back(8);
				ReplaceWord('g', str);
				break;
			case 'o':
				vecInt.push_back(1);
				ReplaceWord('o', str);
				break;
			case 'r':
				vecInt.push_back(3);
				ReplaceWord('r', str);
				break;
			case 'f':
				vecInt.push_back(5);
				ReplaceWord('f', str);
				break;
			case 's':
				vecInt.push_back(7);
				ReplaceWord('s', str);
				break;
			case 'i':
				vecInt.push_back(9);
				ReplaceWord('i', str);
				break;
			default:
				break;
			}
		}
	}

	return true;
}

int main_jiemi()
{
	std::string str;
	while(getline(std::cin, str))
	{
		transform(str.begin(), str.end(), str.begin(), ::tolower);
		std::vector<int> vecInt;
		char specialNum[4] = {'w', 'u', 'x', 'g'};
		for (int i = 0; i < 4; i++)
		{
			ContainSpecialNum(str, vecInt, specialNum[i]);
		}

		//one
		ContainSpecialNum(str, vecInt, 'o');

		//three
		ContainSpecialNum(str, vecInt, 'r');

		//five
		ContainSpecialNum(str, vecInt, 'f');

		//seven
		ContainSpecialNum(str, vecInt, 's');

		//nine
		ContainSpecialNum(str, vecInt, 'i');

		int curInt = 0;
		for (int i = 0; i < 6; i++)
		{
			for (int j = i+1; j < 6; j++)
			{
				if(vecInt[i] > vecInt[j])
				{
					int tmp = vecInt[i];
					vecInt[i] = vecInt[j];
					vecInt[j] = tmp;
				}
			}

		}

		std::cout<<"密码是:";
		for (int i = 0; i < vecInt.size(); i++)
		{
			std::cout<<vecInt[i];
		}

	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/tianzhiyi1989sq/article/details/94546096