Word flip:
Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Input format
input includes an English sentence.
Output format output
the words in reverse order according to the order of the words
Sample input
I love you
Sample output
you love I
Data size and convention
simple string manipulation
Thinking process:
The main problem of this question is that there are spaces when inputting. The getline() function is used, which is used to receive a line of data, including spaces. Then I faced the problem of output in reverse order. At first I used the multiset container to do it. After writing, I found that it would automatically sort string data according to the lexicographic order, and would not store it in the order of reception. Then there was no way to use it. Queue to do it.
Problem solving method:
- Declare the queue (queue)
I don’t know where the queue is visible: https://blog.csdn.net/weixin_49243150/article/details/113338393
- Get the data entered by the user and store them in the queue in the order of one word
- Use loops and queues to enter and dequeue to achieve reverse output
Source program:
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
queue<string>m;//队列
string s;//用来获取用户输入的数据
string stu = "";//用来获取一个一个的单词(不包括空格)
while (getline(cin, s))//getline()函数
{
for (int i = 0; i < s.size(); i++)
{
if (s[i] != ' ')
{
stu = stu + s[i];
}
if (s[i] == ' ' || i == s.size() - 1)
{
m.push(stu);//将空格前面的单词存入队列
stu = "";//初始化为空
}
}
break;//执行玩一遍后退出,因为只输入了一行数据
}
while (m.size())
{
for (int i = 0; i < m.size() - 1; i++)
{
m.push(m.front());//将头元素入队
m.pop();//删除头元素
}
cout << m.front() << " ";
m.pop();//将输出完毕的元素删除
}
}
Evaluation results:
Article flip:
Resource limit
Time limit: 1.0s Memory limit: 128.0MB
Problem description
Enter a paragraph of English without punctuation, and output this paragraph in reverse order in units of words
The input format is
a paragraph of English (no line breaks in the paragraph), ending with a line break.
Output format A
paragraph of English (no newline in the paragraph), ending with a newline character.
Sample input
aab TTR bbc loV DDE Znr CCD
Sample output
CCD Znr DDE loV bbc TTR aab
Data size and agreement
60% of the data guarantee that the word length does not exceed 20 and the character length does not exceed 10^3
100% of the data guarantee that the word length does not exceed 10 5 and the character length does not exceed 10 6
Description:
The article flipping and word flipping in this question is an idea. For article flipping, the number of words is more, which means that the number of queues entering and leaving the queue will increase exponentially, but I took the code corresponding to the word flip and tried it. , The time efficiency is 421ms, which is much larger, but it does not exceed the limit of the problem, so I did not optimize the program.
Source program:
#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
queue<string>m;//队列
string s;//用来获取用户输入的数据
string stu = "";//用来获取一个一个的单词(不包括空格)
while (getline(cin, s))//getline()函数
{
for (int i = 0; i < s.size(); i++)
{
if (s[i] != ' ')
{
stu = stu + s[i];
}
if (s[i] == ' ' || i == s.size() - 1)
{
m.push(stu);//将空格前面的单词存入队列
stu = "";//初始化为空
}
}
break;//执行玩一遍后退出,因为只输入了一行数据
}
while (m.size())
{
for (int i = 0; i < m.size() - 1; i++)
{
m.push(m.front());//将头元素入队
m.pop();//删除头元素
}
cout << m.front() << " ";
m.pop();//将输出完毕的元素删除
}
cout << endl;
}
Evaluation results:
supplement:
The time efficiency of article flipping is relatively low, and there will be time to change the method and find a relatively better solution, so stay tuned.