首页 > ACM题库 > HDU-杭电 > Hdu 1591 Encoded Love-letter-字符串[解题报告] C++
2013
12-12

Hdu 1591 Encoded Love-letter-字符串[解题报告] C++

Encoded Love-letter

问题描述 :

After Gardon had got Angel’s letter, he found it was encoded…Oh my god, why did she encode a love-letter?? But don’t worry, she wrote the algorithm for encoding after the letter:
Each charactor are changed to a corresponding charactor. If the keyword is “Angel”, the rule will be:ABCDEFGHIJKLMNOPQRSTUVWXYZ
ANGELZYXWVUTSRQPOMKJIHFDCBYou may find that in the bottom line, charactors of the keyword come first. All other charactors will come in a reversed order.

Now given another keyword, work the letter out!
Can you write a program to translate the letter?

输入:

The letter will begin with the keyword (All uppercase), then lines of text.

输出:

Decode the letter and print it out. Please note that a upper-case charactor will be decoded to a upper-case charactor, while a lower-case charactor will be decoded to a lower-case charactor.

样例输入:

ANGEL
Fxlr jxaj eac W xlam cqim hqwgl
W xahl kqsl kplgwat zlltwry
Tlj sl atfack jxwru W eqr'j farra zqmylj cqi
W mlslsnlm aj jxl eac
Cqi aml atfack qr sc swre
Lhlrjxqiyx W vikj gar jxwru anqij cqi
Wz jxl eac wr jxl zijiml
Jxwk tqhl fwtt nlgqswry jmil
W'hl rlhlm gxaryl sc swre jxaj W fwtt tqhl cqi zqmlhlm
W eqr'j gaml xqf zqqt wj wk
W fwtt tlj sc emlas gqsl jmil
W fwtt jltt cqi kqsljxwry W farra tlj cqi urqf, W tlj cqi urqf

W tqhl cqi, tqhwry cqi, ak jxl sqikl tqhlk jxl mwgl
Lhlr lhlmc eac xak kjqms, W fwtt atfack nc cqim kwel
W swkk cqi, swkkwry cqi
W eqr'j gaml xqf xame wj wk
W vikj farj cqi jq nl xappc
Lhlmcjxwry, W eq wj zqm cqi

样例输出:

When that day I hear your voice
I have some special feeling
Let me always think I don't wanna forget you
I remember at the day
You are always on my mind
Eventhough I just can think about you
If the day in the future
This love will becoming true
I've never change my mind that I will love you forever
I don't care how fool it is
I will let my dream come true
I will tell you something I wanna let you know, I let you know

I love you, loving you, as the mouse loves the rice
Even every day has storm, I will always by your side
I miss you, missing you
I don't care how hard it is
I just want you to be happy
Everything, I do it for you


题目大意:
转换的规则是根据输入的字符串来确定的,并且我们输入的字符串对应ABC。。。其他则是从反向来,ZYX。。
YY:题目很重要,刚开始自以为是转换的规律是不变的,但这题就不是,所以磨了很久都不知道是怎么回事,所以碰到想不通的,最好把题目再看几遍。还有就是文件输入的一个问题,找了很久,还是波神找出来的,文件后面多了空行,没注意,一直找程序的问题,后面就是在这卡了。。。
但是后来一交wa,就蒙了,吃晚饭回来一看就改正了,真是。。。什么都乱了,为了不慌,多看几遍题目。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ch[27],Bch[27],Sch[27];
int temp[27];
int get_Bch(char x)
{
    int i=0;
    for(i=0;i<26;i++)
        if(Bch[i]==x)
            return i;
}
int get_Sch(char x)
{
    int i=0;
    for(i=0;i<26;i++)
        if(Sch[i]==x)
            return i;
}
int main()
{
    int i,len;    
//    freopen("d:\\1.txt","r",stdin);
    gets(ch);
    memset(temp,0,sizeof(temp));
    len=strlen(ch);
    int h;
    for(i=0;i<len;i++)
    {
        Bch[i]=ch[i];
        h=(int)(ch[i]-65);
        temp[h]=1;
    }
    int j;
    for(i=25,j=0;i>=len;i--,j++)
    {
        while(temp[j]==1)
            j++;
        Bch[i]=j+65;
    }
    for(i=0;i<26;i++)
        Sch[i]=Bch[i]+32;
    char ch1[10000];
    while(gets(ch1))
    {
        i=0;
        while(ch1[i]!='\0')
        {
            if(ch1[i]>='A'&&ch1[i]<='Z')
                printf("%c",(get_Bch(ch1[i])+65));
            else if(ch1[i]>='a'&&ch1[i]<='z')
                printf("%c",(get_Sch(ch1[i])+97));
            else
                printf("%c",ch1[i]);
            i++;
        }
        printf("\n");
    }
    return 0;
}

转自:http://hi.baidu.com/jerrynbcl/item/523fc0dda21e6cf593a97479


  1. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  2. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”

  3. 有一点问题。。后面动态规划的程序中
    int dp[n+1][W+1];
    会报错 提示表达式必须含有常量值。该怎么修改呢。。

  4. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c

  5. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  6. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.