首页 > ACM题库 > HDU-杭电 > HDU 1062 Text Reverse-字符串-[解题报告] Cpp
2013
11-26

HDU 1062 Text Reverse-字符串-[解题报告] Cpp

Text Reverse

问题描述 :

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

输入:

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

输出:

For each test case, you should output the text which is processed.

样例输入:

3
olleh !dlrow
m'I morf .udh
I ekil .mca

样例输出:

hello world!
I'm from hdu.
I like acm.

Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;

int T;
char str[1000];
int begin,end;


int main()
{
    while(scanf("%d",&T)!=EOF)
    {
          getchar();
          for(int i = 1;i <= T; i++)
          { 
                 gets(str);
                 begin = end = 0;
                 while(str[end] != '\0')
                 {
                        if(str[end] == ' ')
                        {
                             for(int j = end-1; j >= begin; j--)
                             {
                                     printf("%c",str[j]);
                                     
                             }
                             printf(" ");
                             begin = end + 1;
                        }
                        end++;
                  
                 }
                 if(str[end] == '\0')
                 {
                             for(int j = end-1; j>= begin; j--)
                             {
                                     printf("%c",str[j]);
                             }
                 }
                 cout<<endl; 
           }
    }
    return 0;
}


  1. 额楼主能否在发布代码的同时对解题思路做个讲解呢?这样大家在学习的时候就方便多了。

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

  3. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥