ACM省选赛 1001 J2J Encryption

来源:互联网 发布:学校网络限制玩游戏 编辑:程序博客网 时间:2024/05/29 19:05
John and Jack are good friends, and both of them like adventures, but their parents do not want them to make troubles here and there, after their making numerous times of troubles, their parents had to forbid the frequency of their meetings. They could only tell each other the time and place of next adventure through letters.
Of course, sometimes, they just write graffiti, to confuse their parents who were in deep curiosity.

To stop their parents from being aware of the contents of their letters, clever John and Jack invented an encryption rule that belongs only to them: J2J Encryption.

The J2J Encryption was as follows:

Only three types of characters--uppercase, lowercase letters and numbers could appear in their character string, and the number of characters in the string should be less than 100.

If one had to put I at the ith position of the string, then put it at the nearest position that was after I and had the same character with it. The last one of each type should be put at the first position of the same type of the character string. If there was only one character in a tyre, no change would be needed.

For example: If there were only three lowercase letters a,b and c in the character string, and they were at the second, forth and sixth position, when encrypted, a was at the forth position, b at the sixth, and c at the second.

Uppercase letters and numbers were under the same encryption rule.

John had written the letter to Jack, now please encrypt it.

Input
First line is an integer n indicate there are n test cases, then n line strings followed.

Output
Output the encrypted string.

Sample input

3
a1W
abc
12a2bRcX

Sample output

a1W
cab
21c2aXbR
郁闷就差一题晋级^^^^^
回来还是写了一下
#include "iostream"
#include <ctype.h>
#include "string"
using namespace std;
char*cstr=NULL;
char*cstrNum=NULL;
char*cstrLow=NULL;
char*cstrUpp=NULL;
void Move(string s,char *ctemp)
{
 
 cstr=ctemp;
 strcpy(cstr,s.c_str());
 char ch=cstr[s.length()];
 for (int i=s.length()-1;i>=0;i--)
 {
  cstr[i+1]=cstr[i];
 }
 cstr[0]=cstr[s.length()];
 cstr[s.length()]='/0';
 
}

void main()
{
 int n,i;
 cin>>n;
 for (i=0;i<n;i++)
 {
  string str,strNum,strLow,strUpp;
  char*c = new char[str.length() + 1];
  cin>>str;
  // c=*(str.c_str());
  strcpy(c,str.c_str());
  for (int j=0;j<str.length();j++)
  {
   if (isdigit(c[j]))
   {
    strNum+=c[j];
    
   }
   if (islower(c[j]))
   {
    strLow+=c[j];
    
   }
   if (isupper(c[j]))
   {
    strUpp+=c[j];
   
   }
   
  }
  int coutNum=0,coutLow=0,coutUpp=0;
  cstrNum = new char[str.length() + 1];
  
  cstrLow = new char[str.length() + 1];
  cstrUpp = new char[str.length() + 1];
  Move(strNum,cstrNum);
  Move(strLow,cstrLow);
  Move(strUpp,cstrUpp);
  for (j=0;j<str.length();j++)
  {
   if (isdigit(c[j]))
   {
    cout<<cstrNum[coutNum];
    coutNum++;
   }
   if (islower(c[j]))
   {
    cout<<cstrLow[coutLow];
    coutLow++;
    
   }
   if (isupper(c[j]))
   {
     cout<<cstrUpp[coutUpp];
    coutUpp++;
    
   }
   
  }
  cout<<endl;
 }
 delete[]cstrNum;
 delete[]cstrLow;
 delete[]cstrUpp;
}