admin管理员组文章数量:1576839
How do you remove consecutive spaces in a simple editor like notepad in Microsoft Windows? One way is to repeatedly "replace all" two consecutive spaces with one space (we call it an action). In this problem, you're to simulate this process and report the number of such "replace all" actions.
For example, if you want to remove consecutive spaces in "A very big joke.", you need two actions:
Input
The input contains multiple test cases, one in a separate line. Each line contains letters, digits, punctuations and spaces (possibly leading spaces, but no trailing spaces). There will be no TAB character in the input. The size of input does not exceed 1MB.Output
For each line, print the number of actions that are required to remove all the consecutive spaces.Sample Input
A very big joke. Goodbye!
Output for Sample Input
2 4
Explanation
If you can't see clearly, here is the sample input, after replacing spaces with underscores:A*very**big****joke. *********Goodbye!
Rujia Liu's Present 5: Developing Simplified Softwares
Special Thanks: Youzhi Bao
题意是给出的字符串有若干连续的空格,每次将每个连续空格中的空格两两合并,要多少步才能使得每个区间只有一个空格,我们先找出区间最长的空格数,由于奇数必须会有多于的一个,于是我们可以将奇数看做偶数处理
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char str[1024*1024+5];//1M
int main()
{
int len,i;
int maxn,space,ans;
while(gets(str))
{
len = strlen(str);
maxn = 0;
space = 0;
for(i = 0;i<len;i++)
{
if(str[i] == ' ')
space++;//连续空格
else
{
if(maxn<space)//找出最长的连续空格
maxn = space;
space = 0;
}
}
ans = 0;
while(maxn!=1)
{
if(maxn%2)//奇数多出一个,可以将多出的也看成是两个空格合并的过程
maxn++;
maxn/=2;
ans++;
}
printf("%d\n",ans);
}
return 0;
}
版权声明:本文标题:BNU20834:Excessive Space Remover 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1727797503a1130421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论