learn how to use cin.getline().
I haven't considered the cases when input is " " , only one word whose length is 500000, and one word with many spaces behind it;
https://pintia.cn/problem-sets/14/problems/812
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string.h>
using namespace std;
char map[5000][200];
char str[500001];
int main() {
int i, j;
int rcnt;
cin.getline(str, 500001);
rcnt = strlen(str);
for (i = 0; i < strlen(str); i++) {
if (str[i] != ' ')
break;
}
int first = i;
for (i = strlen(str) - 1; i >= 0; i--) {
if (str[i] == ' ') {
rcnt = i;
}
else {
while (i >= 0 && str[i] != ' ') {
i--;
}
for (j = i + 1; j < rcnt; j++) {
printf("%c", str[j]);
}
if (i >= first)
printf(" ");
rcnt = i;
}
}
printf("\n");
return 0;
}
网友评论