UVa Problem Solution: 848 - Fmt
2008-11-17 12:59
519 查看
At the first look, this problem seems to be tricky to solve. However, if you implement all the specifications precisely, it would be already solved. There's no need to "translate" the problem's specifications.
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 848 C++ "Fmt" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linesize = 72;
int const buffsize = 2*1024*1024;
int fmt(char *src, int isize, char *dest)
{
int osize = 0;
for (int first = 0, last = first; first < isize; first = last) {
for (int next = last, nonblank = 0, word = 0; last < isize; last = next) {
// search for the next word
for (; next < isize && src[next] != ' ' && src[next] != '/n'; ++next) {
++nonblank;
}
++word;
// keep line break
if (next < isize && src[next] == '/n' &&
(src[next+1] == ' ' || src[next+1] == '/n' ||
nonblank == 0 || next + 1 == isize)) {
last = next + 1;
break;
}
// break the line
if (next - first > linesize) {
// exhaust all blanks
for (last = word > 1 ? last : next + 1; src[last] == ' '; ++last) {}
src[last-1] = '/n';
break;
}
src[next++] = ' '; // eliminate newline
}
copy(src + first, src + last, dest + osize);
osize += last - first;
if (dest[osize-1] == '/n') { // eliminate trailing spaces
for (; osize > 1 && dest[osize-2] == ' '; --osize) {}
dest[osize-1] = '/n';
}
}
return osize;
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
char src[buffsize];
char dest[buffsize];
cin.read(src, buffsize);
int isize = cin.gcount();
int osize = fmt(src, isize, dest);
cout.write(dest, osize);
return 0;
}
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 848 C++ "Fmt" */
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linesize = 72;
int const buffsize = 2*1024*1024;
int fmt(char *src, int isize, char *dest)
{
int osize = 0;
for (int first = 0, last = first; first < isize; first = last) {
for (int next = last, nonblank = 0, word = 0; last < isize; last = next) {
// search for the next word
for (; next < isize && src[next] != ' ' && src[next] != '/n'; ++next) {
++nonblank;
}
++word;
// keep line break
if (next < isize && src[next] == '/n' &&
(src[next+1] == ' ' || src[next+1] == '/n' ||
nonblank == 0 || next + 1 == isize)) {
last = next + 1;
break;
}
// break the line
if (next - first > linesize) {
// exhaust all blanks
for (last = word > 1 ? last : next + 1; src[last] == ' '; ++last) {}
src[last-1] = '/n';
break;
}
src[next++] = ' '; // eliminate newline
}
copy(src + first, src + last, dest + osize);
osize += last - first;
if (dest[osize-1] == '/n') { // eliminate trailing spaces
for (; osize > 1 && dest[osize-2] == ' '; --osize) {}
dest[osize-1] = '/n';
}
}
return osize;
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
char src[buffsize];
char dest[buffsize];
cin.read(src, buffsize);
int isize = cin.gcount();
int osize = fmt(src, isize, dest);
cout.write(dest, osize);
return 0;
}
相关文章推荐
- UVa Problem 848 Fmt (Fmt 程序)
- UVa Problem Solution: 10196 - Check The Check
- UVa Problem Solution: 10252 - Common Permutation
- UVa Problem Solution: 10041 - Vito's Family
- UVa Problem Solution: 10183 - How many fibs?
- UVa Problem Solution: 10157 - Expressions
- UVa Problem Solution: 10090 - Marbles
- UVa Problem Solution: 100 - The 3n+1 problem
- UVa Problem Solution: 10142 - Australian Voting
- UVa Problem Solution: 10149 - Yahtzee
- UVa Problem Solution: 10138 - CDVII
- UVa Problem Solution: 10018 - Reverse and Add
- UVa Problem Solution: 10213 - How Many Pieces of Land?
- UVa Problem Solution: 10089 - Repackaging
- UVa Problem Solution: 10189 - Minesweeper
- UVa Problem Solution: 10038 - Jolly Jumpers
- UVa Problem Solution: 10082 - WERTYU
- UVa Problem Solution: 850 - Crypt Kicker II
- UVa Problem Solution: 120 - Stacks of Flapjacks
- UVa Problem Solution: 701 - The Archaeologist's Dilemma