注:最近这一系列ACM的内容,都是2年多之前的代码,自己回顾一下。
每年夏天,ACM集训队都会进行长期的集中训练,一天到晚在机房看点脑,实在太无聊。所以这天yicou想出了一种新的语言:反转文! 反转文词法将一切文字分为3类:单词,非单词,‘#’号 所有以字母开头的词都是单词(无论后面紧跟什么字符,一直到‘#’结束),非字母开头的都是非单词,而‘#’就还是‘#’。一段普通文章的反转文如下: 原文章:abcd##edf.!#+- 反转文:-+#edf.!##abcd 也就是在保持单词的字符序不变的同时,其他全反向(单词、非单词的排列顺序和中间‘#’全反向,非单词内部字符序也反向) 注意:最后一个如果是单词或非单词,没有’#’。
Input
单组测试数据 只有一行,一个字符串 暨原文章(长度小于1200)。字符串中不包含空格
Output
输出该文章的反转文
Sample Input
Question:#In#the#1600's#when#the#Spanish#moved#into#what
Sample Output
what#into#moved#Spanish#the#when#s'0061#the#In#Question:
Source
yicou
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { string str; int end, front, temp; int len; cin >> str; len = str.length(); end = temp = len-1; front = end - 1; while (front >= 0) { if (str[end] == '#') { printf("#"); end--; temp = end; front = end - 1; } else { if (str[front] != '#') { temp = front; front --; } else { if (('a'<=str[temp]&&str[temp]<='z') || ('A'<=str[temp]&&str[temp]<='Z')) for (int i = temp; i <= end; i++) printf("%c", str[i]); else for (int i = end; i >= temp; i--) printf("%c", str[i]); end = temp - 1; temp = end; front--; } } } if (('a'<=str[temp]&&str[temp]<='z') || ('A'<=str[temp]&&str[temp]<='Z')) for (int i = temp; i <= end; i++) printf("%c", str[i]); else for (int i = end; i >= temp; i--) printf("%c", str[i]); cout << endl; // system("pause"); return 0; }