描述
加密一条信息需要三个整数码, k1, k2 和 k3。字符[a-i] 组成一组, [j-r] 是第二组, 其它所有字符 ([s-z] 和下划线)组成第三组。 在信息中属于每组的字符将被循环地向左移动ki个位置。 每组中的字符只在自己组中的字符构成的串中移动。解密的过程就是每组中的字符在自己所在的组中循环地向右移动ki个位置。
例如对于信息 the_quick_brown_fox 以ki 分别为 2, 3 和 1蔼进行加密。加密后变成 _icuo_bfnwhoq_kxert。下图显示了右旋解密的过程。
观察在组[a-i]中的字符,我们发现{i,c,b,f,h,e}出现在信息中的位置为{2,3,7,8,11,17}。当k1=2右旋一次后, 上述位置中的字符变成{h,e,i,c,b,f}。下表显示了经过所有第一组字符旋转得到的中间字符串,然后是所有第二组,第三组旋转的中间字符串。在一组中变换字符将不影响其它组中字符的位置。
所有输入字符串中只包含小写字母和下划线(_)。所有字符串最多有偿服务0个字符。ki 是1-100之间的整数。输入
输入包括一到多组数据。每个组前面一行包括三个整数 k1, k2 和 k3,后面是一行加密信息。输入的最后一行是由三个0组成的。
输出
对于每组加密数据,输出它加密前的字符串。
样例输入
2 3 1 _icuo_bfnwhoq_kxert 1 1 1 bcalmkyzx 3 7 4 wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji 2 4 3 cjvdksaltbmu 0 0 0样例输出
the_quick_brown_fox abcklmxyz the_quick_brown_fox_jumped_over_the_lazy_dog ajsbktcludmv
分析:这道题主要的难点在于理解,理解了题目要求之后问题就迎刃而解了。因为相关的逻辑很简单。
首先题目中的[a-i]这个要理解为从a到i,中间的不是减号(我刚开始纳闷了好久)
其次字符串虽然经过了3次加密,但是加密的规则是相同的。
最后“所有字符串最多有偿服务0个字符”我把它理解为可以存在 字符串中a-i之间的字符不存在。例如字符串“uuuuuuuuuu”
因为没有说明字符串的长度,因此声明了STL的vector来存储字符的位置。
代码有些臃肿,因为有很多重复的东西。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void jiemi(string str,int a,int b,int c) {
vector<int>hello;
int yushu;
string linshi = str;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'a'&&str[i] <= 'i')
hello.push_back(i + 1);//里面存的是位置信息
}
if (hello.size() != 0)
{
yushu = a % hello.size();
for (int i = 0; i < hello.size(); i++)
{
if (yushu + i >= hello.size())//是否都等于
{
linshi[hello[i + yushu - hello.size()] - 1] = str[hello[i] - 1];
}
else
{
linshi[hello[i + yushu] - 1] = str[hello[i] - 1];
}
}
}
hello.clear();
//第一组解密完成
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'j'&&str[i] <= 'r')
hello.push_back(i + 1);
}
if (hello.size() != 0)
{
yushu = b % hello.size();
for (int i = 0; i < hello.size(); i++)
{
if (yushu + i >= hello.size())
{
linshi[hello[i + yushu - hello.size()] - 1] = str[hello[i] - 1];
}
else
{
linshi[hello[i + yushu] - 1] = str[hello[i] - 1];
}
}
}
hello.clear();
//第二组完成
for (int i = 0; i < str.length(); i++)
{
if ((str[i] >= 's'&&str[i] <= 'z')||str[i]=='_')
hello.push_back(i + 1);
}
if (hello.size() != 0)
{
yushu = c % hello.size();
for (int i = 0; i < hello.size(); i++)
{
if (yushu + i >= hello.size())
{
linshi[hello[i + yushu - hello.size()] - 1] = str[hello[i] - 1];
}
else
{
linshi[hello[i + yushu] - 1] = str[hello[i] - 1];
}
}
}
cout << linshi << endl;
}
int main()
{
string password;
int a, b, c;
cin >> a >> b >> c;
while (a!=0&&b!=0&&c!=0)
{
cin >> password;
jiemi(password, a, b, c);
cin >> a >> b >> c;
}
}