Punctuation CodeForces - 147A

文章讲述了如何使用C++标准库中的stack来解决一个编程问题,要求在给定文本中正确添加空格以满足特定格式规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.

Your task is to add spaces to the text by the following rules:

  • if there is no punctuation mark between two words, then they should be separated by exactly one space
  • there should be no spaces before each punctuation mark
  • there should be exactly one space after each punctuation mark

It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter.

Input

The input data contains of a single non-empty line — the text whose length is no more than 10000 characters.

Output

Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter.

Examples:

Input:

galileo galilei was an   italian physicist  ,mathematician,astronomer
Output:

galileo galilei was an italian physicist, mathematician, astronomer
Input:

galileo  was  born  in  pisa
Output:

galileo was born in pisa

翻译:

您将获得一个由小写拉丁字母、空格和标点符号(点、逗号、感叹号和问号)组成的文本。单词被定义为连续的拉丁字母序列。

您的任务是按照以下规则向文本添加空格:

  • 如果两个单词之间没有标点符号,那么它们应该正好用一个空格隔开
  • 每个标点符号前不应有空格
  • 每个标点符号后面应该有一个空格

保证任意两个标点符号之间至少有一个单词。文本以拉丁字母开头和结尾。

输入

输入数据 包含一个非空行,即长度不超过 10000 个字符的文本。

输出

打印文本,根据规则进行编辑。在这个问题中,你应该非常严格地遵循输出格式。例如,输出行末尾的额外空格被视为错误答案。请注意,行尾的换行符无关紧要。

就是想了很久,因为当时在csdn上搜不到,搜到了我也看不懂(扎心➳♥゛)(还因为看见别人能写出来了,自己也不甘心,就尝试做一下),我用的是stl容器中的栈(stack),因为它可以删除元素(减少空格),也可以增加元素,增加空格,我感觉挺方便的,就用它写出来了,就是需要注意的是,stack不能访问整个栈内元素,只能访问栈顶元素

我说一下我做这道题的思路,就是如果没有遇见空格就压入栈中,这里需要注意的是,如果此时遇见的是标点符号,需要判断一下栈顶元素是不是空格,如果是就删除那个空格,不是的话就不用管了,之后照常将标点符号压入栈内这里才仅仅满足第二个条件(题目要求标点符号前不能有空格)

如果遇见空格了,需要判断一下前面的字符是不是空格,如果是空格就删除栈顶元素(就前一个空格),压入此时遇见的空格,如果前一个元素不是空格,将当前遇见的那个空格压入栈内,这里满足第一个条件(两个单词之间只有一个空格)

如果栈顶元素是标点符号,需要往后面加一个空格,满足第三个条件(每个标点符号后面都应该有一个空格)

生活不易,全靠一口气~_~

别我写了这么多你却在想什么是stl容器栈(stack),那就需要你自己去学了^_^

还要注意的是,输入字符串时不能用cin,它遇见空格会自动结束,用getline()更好

上代码!

#include<bits/stdc++.h>
using namespace std;
stack<char>q;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
    char t[10010];
	string str;
	getline(cin,str);//不能用cin
	int len=str.length();
	for(int i=0;i<len;i++)
	{
		if(str[i]!=' ')
		{
			if(str[i]==','||str[i]=='.'||str[i]=='!'||str[i]=='?')
			{
				if(q.top()==' ')//第二个条件,标点符号前不能有空格
					q.pop();
			}
			q.push(str[i]);
		}	
		else
		{
			if(q.top()==' ')//不能有两个空格,第一个条件
			{
				q.pop();
				q.push(str[i]);
			}
			else
				q.push(str[i]);
		}
		if(q.top()==','||q.top()=='.'||q.top()=='!'||q.top()=='?')
			q.push(' ');//第三个条件,每个标点符号后面有一个空格
	}
	int n=q.size();
	for(int i=0;i<n;i++)
	{
		t[i]=q.top();//将栈中元素存到t数组中
		q.pop();
	}
	for(int i=n-1;i>=0;i--)
		cout<<t[i];
	return 0;
} 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值