vva 709 &poj 1093 - Formatting Text(dp의 흰둥이 일단락)
709 - Formatting Text
Writings e-mails is fun, but, unfortunately, they do not look very nice, mainly because not all lines have the same lengths. In this problem, your task is to write an e-mail formatting program which reformats a paragraph of an e-mail (e.g. by inserting spaces) so that, afterwards, all lines have the same length (even the last one of each paragraph).
The easiest way to perform this task would be to insert more spaces between the words in lines which are too short. But this is not the best way. Consider the following example:
****************************
This is the example you are
actually considering.
Let us assume that we want to get lines as long as the row of stars. Then, by simply inserting spaces, we would get
****************************
This is the example you are
actually considering.
But this looks rather odd because of the big gap in the second line. By moving the word ``are'' from the first to the second line, we get a better result:
****************************
This is the example you
are actually considering.
Of course, this has to be formalized. To do this, we assign a badness to each gap between words. The badness assigned to a gap of n spaces is (n - 1)2. The goal of the program is to minimize the sum of all badnesses. For example, the badness of the first example is 1 + 72 = 50 whereas the badness of the second one is only 1 + 1 + 1 + 4 + 1 + 4 = 12.
In the output, every line has to start and to end with a word. (I.e. there cannot be a gap at the beginning or the end of a line.) The only exception to this is the following:
If a line contains only one word this word shall be put at the beginning of the line, and a badness of 500 is assigned to this line if it is shorter than it should be. (Of course, in this case, the length of the line is simply the length of the word.)
Input
The input file contains a text consisting of several paragraphs. Each paragraph is preceded by a line containing a single integer n, the desired width of the paragraph ( ).
Paragraphs consist of one or more lines which contain one or more words each. Words consist of characters with ASCII codes between 33 and 126, inclusive, and are separated by spaces (possibly more than one). No word will be longer than the desired width of the paragraph. The total length of all words of one paragraph will not be more than 10000 characters.
Each paragraph is terminated by exactly one blank line. There is no limit on the number of paragraphs in the input file.
The input file will be terminated by a paragraph description starting withn=0. This paragraph should not be processed.
Output
Output the same text, formatted in the way described above (processing each paragraph separately).
If there are several ways to format a paragraph with the same badness, use the following algorithm to choose which one to output: Let A and B be two solutions. Find the first gap which has not the same length in A and B. Donot output the solution in which this gap is bigger.
Output a blank line after each paragraph.
Sample Input
28
This is the example you are
actually considering.
25
Writing e-mails is fun, and with this program,
they even look nice.
0
Sample Output
This is the example you
are actually considering.
Writing e-mails is fun,
and with this program,
they even look nice.
제목: (남의 것을 붙인다)
n을 주고 문장을 한 편 주세요. 몇 개의 단어가 있는데 빈 줄로 끝납니다. 이제 문장을 정렬해서 문장의badness를 작게 해야 합니다.badness의 계산 방법은 한 줄에 한 단어만 있다면 단어의 길이가 n보다 작으면 500 증가하고 n과 같으면 0이다.만약에 한 줄에 여러 단어가 있다면 두 단어 사이의badness=(c-1)^2(c는 두 단어의 빈칸수)는 한 줄마다 길이가 n이고 앞의 단어는 맨 끝, 뒤의 단어는 맨 끝(한 번의 경우를 제외)의 사고방식을 제어해야 한다.
dp[i][j]-i번째 단어를 위치 j에 놓을 때 가장 작은badness입니다.
그러면 dp[i][j]=dp[i+1][j+k]+생성된badness
k의 범위를 세심하게 판단하고 기억화 검색으로 dp를 실현하며 제목의 각종 만족해야 하는 조건을 고려해야 AC가 가능하다.
next[i][j]수 그룹으로 경로 출력을 기록합니다. 한 줄에 한 단어만 있을 때 끝에 빈칸이 없도록 주의하십시오.
코드:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
int n,m,ans,cnt,tot,flag;
int num[10005],sum[10005];
vector<string>v;
char s[10005];
int dp[10001][85],next[10001][85];
int sqr(int x)
{
return x*x;
}
int dfs(int x,int y)
{
if(y+v[x].length()-1>n) return INF;
if(dp[x][y]!=INF) return dp[x][y];
if(x==m)
{
if(y+v[x].length()-1==n) return 0;
else
{
if(y==1) return 500;
return INF;
}
}
int i,j,t,best=INF;
for(i=y+v[x].length()+1;i<=n-v[x+1].length()+1;i++)
{
t=dfs(x+1,i)+sqr(i-y-v[x].length()-1);
if(best>t)
{
best=t;
next[x][y]=i;
}
}
if(y==1)
{
t=dfs(x+1,1)+500;
if(best>t)
{
best=t;
next[x][y]=1;
}
}
if(y+v[x].length()-1==n)
{
t=dfs(x+1,1)+0;
if(best>t)
{
best=t;
next[x][y]=1;
}
}
dp[x][y]=best;
return best;
}
void output(int x,int y)
{
cout<<v[x];
int t=y+v[x].length();
if(t==n+1||next[x][y]==1||x==m)
{
printf("
");
}
else
{
for(int i=t;i<next[x][y];i++)
{
printf(" ");
}
}
// printf("x:%d y:%d
",x,y);
if(x==m) return ;
output(x+1,next[x][y]);
}
int main()
{
int i,j,t;
while(scanf("%d",&n),n)
{
gets(s);
string xx;
v.clear();
v.push_back("yj");
cnt=0;
sum[0]=0;
while(gets(s))
{
if(s[0]=='\0') break ;
xx="";
for(i=0; ;i++)
{
if(s[i]!=' '&&s[i]!='\0')
{
xx+=s[i];
}
else
{
if(xx.length()>0)
{
v.push_back(xx);
}
xx="";
if(s[i]=='\0') break ;
}
}
}
m=v.size()-1;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
dp[i][j]=INF;
}
}
ans=dfs(1,1);
output(1,1);
cout<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.