1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#define INF INT_MAX
#include <iostream>
using std::cin;
using std::endl;
using std::cout;
#include <cstring>
using std::strlen;
const int MAX_L = 65;
int getText(char **text);
void typeFormat(char **text, int size);
void calculateSolution(char **line, int n);
int printSolution(char **line, int *p, int n);
int main()
{
char *text[128];
for (int i = 0; i < 128; i++)
text[i] = new char[128];
int size = getText(text);
typeFormat(text, size);
cin.ignore(255, '\n');
cin.get();
return 0;
}
int getText(char **text)
{
int size = 0;
cout << "Enter line( '-' to terminate): \n";
do
{
gets(text[size]);
size++;
}while (text[size - 1][0] != '-');
size--;
return size;
}
void typeFormat(char **text, int size)
{
char *tokenPtr;
char *line[1024];
int counter = 0;
//Take every word from every line and put it in line array
for (int i = 0; i < size; i++)
{
tokenPtr = strtok(text[i], " ");
while (tokenPtr != NULL)
{
line[counter] = tokenPtr;
counter++;
tokenPtr = strtok(NULL, " ");
}
}
int extraSpace[512][512];
int cost[512][512];
int c[1024], p[1024];
//Calculate all possible extra spaces
for (int i = 1; i <= counter; i++)
{
extraSpace[i][i] = MAX_L - strlen(line[i - 1]);
for (int j = i + 1; j <= counter; j++)
{
extraSpace[i][j] = extraSpace[i][j - 1] - strlen(line[j - 1]) - 1;
}
}
//Calculate every possible costs for lines from i to j
for (int i = 1; i <= counter; i++)
{
for (int j = i; j <= counter; j++)
{
if (extraSpace[i][j] < 0)
cost[i][j] = INF;
else
if (j == counter && extraSpace[i][j] >= 0)
cost[i][j] = 0;
else
cost[i][j] = extraSpace[i][j] * extraSpace[i][j];
}
}
//Get best costs
c[0] = 0;
for (int j = 1; j <= counter; j++)
{
c[j] = INF;
for (int i = 1; i <= j; i++)
{
if (c[i - 1] != INF && cost[i][j] != INF && (c[i - 1] + cost[i][j] < c[j]))
{
c[j] = c[i - 1] + cost[i][j];
p[j] = i;
}
}
}
printSolution(line, p, counter);
}
int printSolution(char **line, int *p, int n)
{
int k;
int l = 0;
if (p[n] == 1)
k = 1;
else
k = printSolution(line, p, p[n] - 1) + 1;
//Get length of line
for (int i = p[n]; i <= n; i++)
{
l += strlen(line[i - 1]) + 1;
}
//Calculate and distribute blank spaces
int extra = MAX_L - l + 1;
int fill[1024];
for (int i = 0; i < 1024; i++)
fill[i] = 0;
while (extra != 0)
{
for (int i = p[n]; i < n; i++)
{
if (extra != 0)
{
fill[i]++;
extra--;
}
else
break;
}
}
//Print line
for (int i = p[n]; i <= n; i++)
{
if (i == p[n])
{
cout << line[i - 1];
for (int j = 1; j <= fill[i]; j++)
cout << " ";
}
else
{
cout << " " << line[i - 1];
for (int j = 1; j <= fill[i]; j++)
cout << " ";
}
}
cout << endl;
return k;
}
|