我的思路是:定义a[500000]、temp[500000]、final[500000]三个数组,a用来存放最开始的字符串,temp把a中多余的空格去掉,变成只有一个空格的句子形式,vector数组final的每个单元都存放一个单词,按句子倒序的单词顺序,依次存放在final[0]到final[n-1],最后把final的单词输出,为什么运行不了并且无法输入呢?
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main()
{
vector<char> final[500000];
char a[500000];
char temp[500000];
cin.getline(a, 500000);
int len = (int)strlen(a);
int loca = 0;
int loct = 0;
int locf = 0;
for(; loca < len;)
{
while(a[loca] == ' ')
loca++;
temp[loct] = a[loca];
loca++;
loct++;
if(a[loca] == ' ')
{
temp[loct] = ' ';
loct++;
}
}
loct--;
int pro;
int end;
pro = loct;
end = loct;
while(pro >= 0)
{
//pro定位到单词开头字母
while(temp[pro] == ' ')
pro--;
if(pro >= 0)
{
while(temp[pro] != ' ')
pro--;
pro++;
}
//end定位到单词结尾字母
end = pro;
while(temp[end] != ' ')
end++;
end--;
for(int i = pro; i <= end; i++)
{
final[locf].push_back(temp[i]);
}
locf++;
pro--;
}
if(final[locf].empty())
locf--;
for(int i = 0; i <= locf; i++)
{
for(int j = 0; j < (int)final[i].size(); j++)
{
cout << final[i][j];
}
if(i != locf)
cout << "#";
}
return 0;
}