Unix 的 fmt 程序可以对文本中的段落进行重排,保证每一行不超过 72 个字符。重排时它会遵守以下规则:
- 新的一行可以从任何一个有空格的地方开始。如果开始一个新行,则在上一行的末尾和新行的开始都不应该有任何空格。
- 在输出时,可以删除段落中的换行。文章的段落是通过一个空白行来划分的。
- 在行尾不应该有任何空格。
- 如果在一长串的字符间没有空格,且其长度超过了 72 个字符,则应该把它们放在一个单独的行上。
Sample Imput
Unix fmt
The unix fmt program reads lines of text, combining
and breaking lines so as to create an
output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.
1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the
end of the previous line or at the beginning of the new line.
2. A line break in the input may be eliminated in the output, provided
it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.
Sample Output
Unix fmt
The unix fmt program reads lines of text, combining and breaking lines
so as to create an output file with lines as close to without exceeding
72 characters long as possible. The rules for combining and breaking
lines are as follows.
1. A new line may be started anywhere there is a space in the input.
If a new line is started, there will be no trailing blanks at the end of
the previous line or at the beginning of the new line.
2. A line break in the input may be eliminated in the output,
provided it is not followed by a space or another line break. If a line
break is eliminated, it is replaced by a space.
Source
Miguel Revilla 2002-06-15
测试输入![关于“测试输入”的帮助](http://online.bit.edu.cn/moodle/theme/image.php/bluebit/core/1348222124/help)
期待的输出![关于“期待的输出”的帮助](http://online.bit.edu.cn/moodle/theme/image.php/bluebit/core/1348222124/help)
时间限制![关于“时间限制”的帮助](http://online.bit.edu.cn/moodle/theme/image.php/bluebit/core/1348222124/help)
内存限制![关于“内存限制”的帮助](http://online.bit.edu.cn/moodle/theme/image.php/bluebit/core/1348222124/help)
额外进程![关于“{$a} 个额外进程”的帮助](http://online.bit.edu.cn/moodle/theme/image.php/bluebit/core/1348222124/help)
测试用例 1 以文本方式显示
- Unix fmt↵
- ↵
- The unix fmt program reads lines of text, combining↵
- and breaking lines so as to create an↵
- output file with lines as close to without exceeding↵
- 72 characters long as possible. The rules for combining and breaking↵
- lines are as follows.↵
- ↵
- 1. A new line may be started anywhere there is a space in the input.↵
- If a new line is started, there will be no trailing blanks at the↵
- end of the previous line or at the beginning of the new line.↵
- ↵
- 2. A line break in the input may be eliminated in the output, provided↵
- it is not followed by a space or another line break. If a line↵
- break is eliminated, it is replaced by a space.↵
以文本方式显示
- Unix fmt↵
- ↵
- The unix fmt program reads lines of text, combining and breaking lines↵
- so as to create an output file with lines as close to without exceeding↵
- 72 characters long as possible. The rules for combining and breaking↵
- lines are as follows.↵
- ↵
- 1. A new line may be started anywhere there is a space in the input.↵
- If a new line is started, there will be no trailing blanks at the end of↵
- the previous line or at the beginning of the new line.↵
- ↵
- 2. A line break in the input may be eliminated in the output,↵
- provided it is not followed by a space or another line break. If a line↵
- break is eliminated, it is replaced by a space.↵
1秒 64M 0
ple Input
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
#include <stdio.h>
#include <string.h>
main()
{
char a[100000];
int i=0,j,n=0,m,s[20000],x=0,y=0,d=0,c;
while((a[i++]=getchar())!=EOF)
{
}
for(j=0;j<i;j++)
{
if((a[j]=='\n')&&(a[j+1]!='\n')&&(a[j-1]!='\n'))
{
a[j]=' ';
}
}
for(m=0;m<i-2;m++)
{
if(m!=0&&a[m-1]!='\n')
{
if(a[m]==' ')
{
for(d=m;d<=m+71;d++)
{if(a[d]!=' ') {m=d;break;}}
}
}
x=m+71;
if(x>=i-2) {x=i-3;}
for(d=m;d<=x;d++)
{
if(a[d]=='\n')
{
for(c=m;c<d;c++)
{printf("%c",a[c]);}
printf("\n");printf("\n");
m=d+1;goto k;
}
}
if(a[x]==' '&&a[x-1]!=' ')
{
for(y=m;y<x;y++)
{
putchar(a[y]);
}
m=x;
printf("\n");
}
else if(a[x-1]==' '&&a[x]==' ')
{
for(y=x-1;1;y--)
{
if(a[y]!=' ') break;
}
for(d=m;d<=y;d++)
{
printf("%c",a[d]);
}
m=x;printf("\n");
}
else
{
if(a[x+1]==' ')
{
for(c=m;c<=x;c++)
{printf("%c",a[c]);}
}
else
{
for(d=x;d>=m;d--)
{
if(a[d]==' ')break;
}
if(d==m-1)
{
for(y=m;;y++){if (a[y]==' ') break;}
for(c=m;c<y;c++) printf("%c",a[c]);
printf("\n");m=y;goto k;
}
for(y=d;y>=m;y--)
{
if(a[y]!=' ') break;
}
for(c=m;c<=y;c++)
{printf("%c",a[c]);}
}
printf("\n");
m=d;
}
k:;
}
}