hdu 1062 Text Reverse 字符串反转

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 18240    Accepted Submission(s): 6900

Problem Description

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.

Input

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.

Output

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

Sample Input

3

olleh !dlrow

m‘I morf .udh

I ekil .mca

Sample Output

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.

字符串的题目。要注意空格的输出,空格也要原样输出。所以之前用stringstream 打的PE了。

#include<stdio.h>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;

int main()
{
	int n;
	stringstream stream;
	string str;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		getline(cin,str);
		int pos=0;
		for(int i=0;i<str.length();i++)
		{
			if(str[i]!=' '&&(i==0||str[i-1]==' '))
				pos=i;

			if((i+1==str.length()||str[i+1]==' ')&&str[i]!=' ')
			{
				for(int j=pos;j<=(i+pos)/2;j++)
				{
					swap(str[i-(j-pos)],str[j]);
				}
			}

		}

		cout<<str<<endl;
	}
	return 0;
}

时间: 2024-12-09 19:12:43

hdu 1062 Text Reverse 字符串反转的相关文章

HDOJ/HDU 1062 Text Reverse(字符串翻转~)

Problem Description 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. Input The input contains several test cases. The first line of the inpu

HDU 1062.Text Reverse【栈或数组或字符串流】【字符处理】【8月30】

Text Reverse Problem Description 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. Input The input contains several test cases. The first lin

HDU 1062 Text Reverse

字符串反转:每个单词反转,然后输出. PE做法:所有单词反转并写入另一个数组中,附代码 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=1005; 6 int main() { 7 char ol[N],no[N]; 8 int n; 9 // freopen("C:\\CODE\\in.txt",

HDU 1062 Text Reverse(水题,字符串处理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. 1 #include<cstdio> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath> 6 #include<deque> 7 #include&

题解报告:hdu 1062 Text Reverse

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题思路:问题求解的是单词的反转... AC代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 char a[1005]; 4 int main() 5 { 6 int T,len,x,y; 7 while(cin>>T){ 8 getchar();//吃掉回车符 9 while(T--){ 10 gets(a); 11

[hdu 1062] Text Reverse | STL-stack

原题 题目大意: t组数据,每组为一行,遇到空格时讲前面的单词反转输出. 题解: 显然的栈题,遇到空格时将当前栈输出清空即可 #include<cstdio> #include<stack> #include<cstring> using namespace std; int t,l; char s[1010]; stack <char> stk; int main() { scanf("%d",&t); getchar(); w

[C/C++] String Reverse 字符串 反转

#include <iostream> #include <string> #include <algorithm> #include <cstring> inline void STL_Reverse(std::string& str) // 反转string字符串 包装STL的reverse() 可以inline { reverse(str.begin(), str.end()); // STL 反转函数 reverse() 的实现 /* tem

hdu1062 Text Reverse(文字反转stack)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 题目描述: 伊格纳修斯喜欢用相反的方式写词.给定由Ignatius编写的单行文本,您应该反转所有单词,然后输出它们. 输入值 输入包含几个测试用例.输入的第一行是单个整数T,它是测试用例的数量.随后是T测试用例.每个测试用例包含一行,并包含多个单词.一行中最多有1000个字符. 输出量 对于每个测试用例,您应该输出已处理的文本. 样本输入 3 olleh  !dlrow m'I  morf 

【HDOJ】1062 Text Reverse

#include<iostream>#include<string>using namespace std;void main(){ int T; while (cin>>T) { getchar(); for (int i = 0; i < T; i++) { string str; getline(cin, str); int end = 0,top = 0; while (str[end] != '\0') { if (str[end]==' ') { fo