文本规范化(字符串模拟)


Problem Link:http://139.129.36.234/problem.php?id=1230


1230: 文本规范化

时间限制: 1 Sec  内存限制: 128 MB
提交: 5  解决: 5
[ 提交][ 状态][ 讨论版]

题目描述

字符串是计算机经常处理的对象。现在有多行文本,文本中只包含小写字母、空格和数字和回车,不存在空行或者全部为空格的行。我们需要将这段文本规范化。要实现下面三个要求:(1)去掉每行的行首多余空格(2)

如果单词中间有多个空格,我们只保留一个空格(3)如果数字和字母相邻,需要在他们之间加下划线'_'。

输入

输入多行上述文本,每一行文本字符不超过100个。为了方便查看空格,空格用#代替

输出

输出满足上面要求的文本,空格用#代替


样例输入

##you#are#boy
you##am#girl
###a40b##c

样例输出

you#am#boy
you#am#girl
a_40_b#c


AC code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<set>
#define LL long long
#define exp 1e-9
#define MAXN 1000010        
using namespace std;

int main( )  
{  
//	freopen("D:\\in.txt","r",stdin); 
	int i,j,fg;
	char s[111];
	char pre;
	while(gets(s))
	{
		i=0;
		while(s[i]=='#')
		{
			i++;
		}
		printf("%c",s[i]);
		pre=s[i];
		i++;
		fg=0;
		while(s[i])
		{
			if(s[i]=='#')
			{
				fg=1;
				printf("#");
				i++;
				while(s[i]=='#')
				{
					i++;
				}
				pre=s[i];
			}
			else
			{
				if((pre>='0'&&pre<='9'&&s[i]>='a'&&s[i]<='z')||(pre>='a'&&pre<='z'&&s[i]>='0'&&s[i]<='9'))
				{
					printf("_%c",s[i]);
					pre=s[i];
					i++;
				}
				else
				{
					printf("%c",s[i]);
					i++;
				}
			}
		}
		puts("");		
	}
	return 0;  
}  



版权声明:本文为Enjoying_Science原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。