nefu 1116 字符串加密

字符串加密

Problem : 1116

Time Limit : 1000ms

Memory Limit : 65536K

description

给你一段经过加密的字符串,我们称之为密文,现在请你编写程序输出它加密前的字符串(原文)。
已知原文中只包含26个小写字母,而密文中除了包含小写字母,还包含0~9十个数字字符,解密规则是:
1、密文中一个字母前面如果有数字x,那么原文的对应位置是在字母表中从这个字母开始向后移动x位得到的新字母。例如2a表示a向后移动2位,原文中对应就是c,再例如1z表示z向后移动1位,原文中对应就是a。
2、密文中一个字母前面如果没有数字,那么原文的对应位置就是该字母。

input

多组输入数据,每组数据只有一个加密后的字符串。长度不超过1000000,只包含数字字符和小写字母,其中连续数字的长度不超过9,最后一位一定是小写字母。

output

输出一行原字符串。

sample_input

happynewyear
25ia1op4un4a1vy3ba24t

sample_output

happynewyear
happynewyear

 

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char *ch="abcdefghijklmnopqrstuvwxyz";
char a[1200000];
int main()
{
    int i,k,j,shu,tmp,cou;
    while(scanf("%s",a)!=-1)
    {
        for(i=0;a[i]!='\0';)
        {
          k=0;shu=0;j=1;
          while(a[i]>='0'&&a[i]<='9')
          {
              k++;
              i++;
          }//此时的i是那个字母
          if(k)
          {
             for(cou=1;cou<=k;cou++)
             {
                 shu=shu+(((int)a[i-cou]-48)*j);
                 j=j*10;
             }
             tmp=(a[i]-97+shu)%26;
             printf("%c",ch[tmp]);
          }
          else cout<<a[i];
          i++;
        }
        cout<<endl;
    }
    return 0;
}

//while。。很有趣。。。

//下面是之前超时的代码

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
char *ch="abcdefghijklmnopqrstuvwxyz";
char a[1200000];
int main()
{
    int k,shu,tmp,i;
    while(scanf("%s",a)!=-1)
    {
        for(i=0;i<strlen(a);i++)
        {
          if(a[i]<='z'&&a[i]>='a')
          {
              if(a[i-1]>'9'||a[i-1]<'0')
              printf("%c",a[i]);
              else
              {
                  shu=(int)a[i-1]-48;
                  if(a[i-2]<='9'&&a[i-2]>='0')
                  shu=shu+((int)a[i-2]-48)*10;
                  tmp=(a[i]-97+shu)%26;
                  printf("%c",ch[tmp]);
              }
          }
        }
        cout<<endl;
    }

    return 0;
}

 

posted @ 2016-02-18 18:26  邻家那小孩儿  阅读(195)  评论(0编辑  收藏  举报