ZOJ 1151 Word Reversal反转单词 (string字符串处理)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151

For each list of words, output a line
with each word reversed without changing the order of the words.

This problem contains
multiple test cases!

The first line of a multiple input is
an integer N, then a blank line followed by N input blocks. Each input block is
in the format indicated in the problem description. There is a blank line
between input blocks.

The output format consists of N
output blocks. There is a blank line between output blocks.

Input

You will be given a number of test
cases. The first line contains a positive integer indicating the number of cases
to follow. Each case is given on a line containing a list of words separated by
one space, and each word contains only uppercase and lowercase
letters.

Output

For each test case, print the output
on one line.

Sample
Input

1

3
I am happy today
To be or not
to be
I want to win the practice contest

Sample
Output

I ma yppah yadot
oT eb ro ton ot
eb
I tnaw ot niw eht ecitcarp tsetnoc

翻译:

对于一串单词,直接把他们输出在一行上,要把每一个单词反转,但每一个单词的位置不要改变;

本程序包括多组測试数据;

输入数据的第一行是一个整数N,然后是一空行,后面跟着N个数据块,每一个数据块的格式在程序描写叙述中说明了,数据块中有一空行。

输出格式由N个输出块组成,每一个输出块之间有一空行;

输入描写叙述:

现给你多组測试数据,第一行是一个正整数,表示接下来測试数据的组数,每组測试占一行,包括一串单词,中间用一个空格隔开,每一个单词仅包括大小写字母;

输出描写叙述:
每组測试数据都打印在一行上;

代码:

#include <iostream>

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

string s, t;
int n, cas,
flag = 0;
char ss[1000];

int main()
{
    cin >>
cas;
    while(cas--) {
        scanf("%d",
&n);
        getchar();
     
  while(n--) {
            cin.getline(ss,
1000);
            s = ss;
   
        flag = 0;
         
  t.clear();
            for(int i=0;
i<s.size(); i++) {
               
if(s[i] != ‘ ‘ && i <= s.size()-1) {
       
            t += s[i];
     
          }else {
         
          reverse(t.begin(), t.end());
   
                if(flag) cout << "
";
                    cout
<< t;
                 
  flag = 1;
               
    t.clear();
             
  }
            }
     
      reverse(t.begin(), t.end());
       
    cout << " " << t << endl;
   
    }
        if(cas) cout << endl;

    }
    return 0;
}

ZOJ 1151 Word Reversal反转单词 (string字符串处理),布布扣,bubuko.com

时间: 2024-08-03 11:13:22

ZOJ 1151 Word Reversal反转单词 (string字符串处理)的相关文章

zoj 1151 Word Reversal(字符串操作模拟)

题目连接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151 题目描述: For each list of words, output a line with each word reversed without changing the order of the words. This problem contains multiple test cases! The first line of a multip

字符串反转&amp;单词反转-小米电视

方法一:先是全字符反转,然后再以空格为界定符反转单词. #include<stdio.h>#include<string.h> string_change(char * p, int start, int end)   //字符反转{        int i,len;        char temp;        len = strlen(p); while(start<=end)        {                temp = p[start];    

Word Break II 求把字符串拆分为字典里的单词的所有方案 @LeetCode

这道题类似  Word Break 判断是否能把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不仅仅是是否能拆分,而是要求出所有的拆分方案.因此用递归. 但是直接递归做会超时,原因是LeetCode里有几个很长但是无法拆分的情况,所以就先跑一遍Word Break,先判断能否拆分,然后再进行拆分. 递归思路就是,逐一尝试字典里的每一个单词,看看哪一个单词和S的开头部分匹配,如果匹配则递归处理S的除了开头部分,直到S为空,说明可以匹配. public class Solution

Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是LeetCode里有几个非常长可是无法拆分的情况.所以就先跑一遍Word Break,先推断能否拆分.然后再进行拆分. 递归思路就是,逐一尝试字典里的每个单词,看看哪一个单词和S的开头部分匹配,假设匹配则递归处理S的除了开头部分,直到S为空.说明能够匹配. Given a string s and a

String字符串处理函数

开发习惯常用字符串处理函数梳理:strtr() 转换字符串中特定的字符.substr() 返回字符串的一部分.strstr() 搜索字符串在另一字符串中的首次出现(对大小写敏感)str_replace() 替换字符串中的一些字符.(对大小写敏感)strcmp() 比较两个字符串.(对大小写敏感)strlen() 返回字符串的长度.substr_count() 计算子串在字符串中出现的次数.substr_replace() 把字符串的一部分替换为另一个字符串 implode() 把数组元素组合为一

PHP中String字符串处理函数完整版

文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/602 开发习惯常用字符串处理函数梳理: strtr() 转换字符串中特定的字符. substr() 返回字符串的一部分. strstr() 搜索字符串在另一字符串中的首次出现(对大小写敏感) str_replace() 替换字符串中的一些字符.(对大小写敏感) strcmp() 比较两个字符串.(对大小写敏感) strlen() 返回字符串的长度. substr_count() 计算子串

python基本操作之列表,元组,string字符串

序列是python中最基本的数据结构,序列中的每一个元素都分配一个数字-他的位置或索引,第一个索引是0,第二个索引是1,以此类推 最常见的序列是列表和元组 列表 是python中最常用的数据类型,可以作为一个方括号内的逗号分隔值出现, list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];

HDU 2072 单词数 --- 字符串处理

/* HDU 2072 单词数 --- 字符串处理 */ #include <cstdio> //C语言改成stdio.h即可 #include <cstring> //C语言改成string.h即可 const int maxn = 85; int main() { char *head1, *head2; char a[maxn]; char b[maxn][maxn]; int i, k, len, cnt1, cnt2; while (gets(a)){ //遇到字符串&q

ZOJ 1115 Digital Roots(简单,字符串与数)

题目 //好一道水水题,可是我居然也错了那么多次,后来百度来发现是因为数据数位可能很长很长,要用字符串数组... //简单 //有坑啊——数据可能很大很大,要用字符串表示! #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char s[1010]; while(scanf("%s",s)!=EOF) { if(strc