首页 > ACM题库 > HDU-杭电 > hdu 1915 Arne Saknussemm-字符串处理-[解题报告]C++
2013
12-27

hdu 1915 Arne Saknussemm-字符串处理-[解题报告]C++

Arne Saknussemm

问题描述 :

Following the account of Jules Verne, a scrambled message written by the middle age alchemist Arne Saknussemm, and deciphered by professor Lidenbrock, started the incredible travel to the center of the Earth. The scrambling procedure used by Arne is alike the procedure given below.

1. Take a non empty message M that contains letters from the English alphabet, digits, commas, dots, quotes (i.e. ‘), spaces and line breaks, and whose last character is different than space. For example, consider the following message whose translation reads "In Sneffels’s crater descend brave traveler, and touch the center of the Earth".

In Sneffels craterem descende audasviator, et terrestre centrum attinges.

2. Choose an integral number 0<K≤length(M) and add trailing spaces to M such that the length of the resulting message, say M’, is the least multiple of K. For K=19 and the message above, where length(M)=74 (including the 8 spaces and the line break that M contains), two trailing spaces are added yielding the message M’ with length(M’)=76.

3. Replace all the spaces from M’ by the character _ (underscore) ; replace all the line breaks from M’ by \ (backslash), and then reverse the message. In our case:

__.segnitta_murtnec_ertserret_te_,rotaiv\sadua_ednecsed_meretarc_sleffenS_nI

4. Write the message that results from step 3 in a table with length(M’)/K rows and K columns. The writing is column wise. For the given example, the message is written in a table with 76/19=4 rows and 19 columns as follows:
[缺少图片]

5. The strings of characters that correspond to the rows of the table are the fragments of the scrambled message. The 4 fragments of Arne’s message given in step 1 are:

_etmneet_t\udsmt_fS.narctrtria_edrrlen
_gtuerr_,asaneeasf_si_t_seeovdec_ecenI

Write a program that deciphers non empty messages scrambled as described. The length of a message, before scrambling, is at most 1000 characters, including spaces and line breaks.

输入:

The program input is from a text file where each data set corresponds to a scrambled message. A data set starts with an integer n, that shows the number of fragments of the scrambled message, and continues with n strings of characters that designate the fragments, in the order they appear in the table from step 4 of the scrambling procedure. Input data are separated by white-spaces and terminate with an end of file.

输出:

The program input is from a text file where each data set corresponds to a scrambled message. A data set starts with an integer n, that shows the number of fragments of the scrambled message, and continues with n strings of characters that designate the fragments, in the order they appear in the table from step 4 of the scrambling procedure. Input data are separated by white-spaces and terminate with an end of file.

样例输入:

4  _etmneet_t\udsmt_fS
   _gtuerr_,asaneeasf_
   .narctrtria_edrrlen
   si_t_seeovdec_ecenI
11 e n r e V _ s e l u J

样例输出:

In Sneffels craterem descende audas
viator, et terrestre centrum attinges.

Jules Verne

 

http://acm.hdu.edu.cn/showproblem.php?pid=1915

 

题目大意:又是一题破译密码的问题啦,这道题就是”_”代表空格,”/”代表是换行,字符串最后一行末尾的空格不要输出,要不然就PE,输出结果就是了。

 

解题思路:很水的一题,自己看代码吧.

 

#include <iostream>
#include <algorithm>
using namespace std;

char date[1000][1000];
char strings[2000];

int main ()
{ 
    int num;
    int i,j;
    int len;
    int pos=0;
    while(cin>>num)
    { 
        pos=0;
        for(i=0;i<num;i++)
            cin>>date[i];
        len=strlen(date[0]);
        int flag=0;
        for(j=0;j<len;j++)
        {
			
            for(i=0;i<num;i++)
            {
                if(date[i][j]!='_')
                    flag=1;            
                if(flag==1)                  
                {
                    strings[pos]=date[i][j];
                    pos++;
                }
            }
            
        }
		strings[pos]='/0';
        reverse(strings,strings+pos);/*反向排列元素*/
        for(i=0;i<pos;i++)
        {
            if(strings[i]=='_')
            {
                printf(" ");
                strings[i]=' ';
                continue;
            }
            else if(strings[i]=='//')
            {
                cout<<'/n';
                continue;
            }
            else 
                cout<<strings[i];
        }
        cout<<endl<<endl;
    }
    return 0;
}

解题转自:http://blog.csdn.net/q3498233/article/details/4441184