Reversed Words
Time Limit: 2000MS Memory limit: 131072K
题目描述
Some aliens are learning English. They have a very strange way in writing that they revered every word in the sentence but keep all the words in common order. For example when they want to write “one two three”, they will write down “eno owt eerht”.
Now we’ve got some sentence written by these aliens, translate them! And maybe we will know some of their secrets!
输入
For each test cases, there will be one line contains only lower case letters and spaces. The length of each line will be no more than 10000. Test cases which are longer than 5000 will be less than 50. Continuous letters are seen as a word, words are separated by spaces. There won’t be two adjacent spaces in the input. Space won’t be the first or the last character.
输出
示例输入
2
eno owt eerht
abcde
示例输出
one two three
edcba
题解: 用栈来进行操作
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int main(){
int T;
cin >> T;
getchar();
stack <char> s;
while (T--){
while(!s.empty())
s.pop();
char c[10005];
gets(c);
int len = strlen(c);
for(int i = 0; i < len ; ++i){
if(c[i] == ' '){
while(!s.empty()){
cout << s.top();
s.pop();
}
cout << " " ;
}
else
s.push(c[i]);
}
while(!s.empty()){
cout << s.top();
s.pop();
}
cout << endl ;
}
return 0;
}
本文介绍了一种使用栈数据结构来实现特殊外星语言句子的翻译方法。这种语言的特点是单词内部字符顺序颠倒,但句子中单词的顺序保持不变。通过逐字符读取输入并利用栈逆序存储字符的方式,最终输出正确翻译后的英文句子。
1348

被折叠的 条评论
为什么被折叠?



