谁能给孕妈推荐一些pp的衣服?# Fashion - 美丽时尚
w*x
1 楼
Print neatly
一下忘了怎么做了,花了半小时,处理下标很痛苦啊。
忽然发现没想的那么复杂。
//you have a list of words, you want to print them
//on a paper with width n. when printing, each
//word must be separated by one blank except the blanks after the last word.
Find the fewest
//possible blanks except the last line.
//f(i, j) = [1 + f(i+1, j+len(i)+1) ] || [nLen - j-len(i) + f(i+1,0)]
int GetLeastBlanks(int a[], int n, int nLen)
{
if (NULL == a || n <= 0 || nLen <= 0)
return -1;
int rec[100][100] = { 0 };
for (int i = n-2; i >= 0; i--)
{
for (int j = nLen-1; j >= 0; j--)
{
if (j + a[i] > nLen)
continue;
int nStartLine = nLen - j - a[i] + rec[i+1][0];
int nCont = INT_MAX;
if (j + a[i] + a[i+1] < nLen)
nCont = 1 + rec[i+1][j+a[i]+1];
rec[i][j] = min(nStartLine, nCont);
}
}
return rec[0][0];
}
一下忘了怎么做了,花了半小时,处理下标很痛苦啊。
忽然发现没想的那么复杂。
//you have a list of words, you want to print them
//on a paper with width n. when printing, each
//word must be separated by one blank except the blanks after the last word.
Find the fewest
//possible blanks except the last line.
//f(i, j) = [1 + f(i+1, j+len(i)+1) ] || [nLen - j-len(i) + f(i+1,0)]
int GetLeastBlanks(int a[], int n, int nLen)
{
if (NULL == a || n <= 0 || nLen <= 0)
return -1;
int rec[100][100] = { 0 };
for (int i = n-2; i >= 0; i--)
{
for (int j = nLen-1; j >= 0; j--)
{
if (j + a[i] > nLen)
continue;
int nStartLine = nLen - j - a[i] + rec[i+1][0];
int nCont = INT_MAX;
if (j + a[i] + a[i+1] < nLen)
nCont = 1 + rec[i+1][j+a[i]+1];
rec[i][j] = min(nStartLine, nCont);
}
}
return rec[0][0];
}