Skip to content

Commit

Permalink
Text justification
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 11, 2017
1 parent 11d51a4 commit aaa4cf0
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 26 deletions.
2 changes: 2 additions & 0 deletions 068_text_justification/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test justification.c
113 changes: 113 additions & 0 deletions 068_text_justification/justification.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void line_fill(char *line, int len, char** words, int *word_lens, int max_size,
int even_spaces, int remain_spaces, int start, int end)
{
int i, j;
char *p = line;
for (i = start; i < end; i++) {
memcpy(p, words[i], word_lens[i]);
p += word_lens[i];
if (i < end - 1) {
for (j = 0; j < even_spaces; j++) {
*p++ = ' ';
}
if (remain_spaces > 0) {
*p++ = ' ';
remain_spaces--;
}
}
}
while (p - line < max_size) {
*p++ = ' ';
}
*p++ = '\0';
}

/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
static char** fullJustify(char** words, int wordsSize, int maxWidth, int* returnSize)
{
int i, j, k, cap = 100, count = 0;
char **lines = malloc(cap * sizeof(char *));
char *buf = malloc(cap * (maxWidth + 1));
for (i = 0; i < cap; i++) {
lines[i] = buf + i * (maxWidth + 1);
}

int *word_lens = malloc(wordsSize * sizeof(int));
for (i = 0; i < wordsSize; i++) {
word_lens[i] = strlen(words[i]);
}

int wc = 0;
int len = 0;
int start = 0;
int chars = 0;
for (i = 0, j = 0; i < wordsSize; i++) {
if (len + word_lens[i] > maxWidth) {
//line_fill(lines[count], len, words, word_lens, maxWidth, chars, wc, start, i);
int even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);
int remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);
count++;
wc = 1;
len = word_lens[i] + 1;
chars = word_lens[i];
start = i;
} else if (len + word_lens[i] == maxWidth) {
chars += word_lens[i];
int even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;
int remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;
line_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);
count++;
wc = 0;
len = 0;
chars = 0;
start = i + 1;
} else {
chars += word_lens[i];
len += word_lens[i] + 1; /* at least one space */
wc++;
}
}

if (wc > 0) {
char *p = lines[count];
for (i = start; i < start + wc; i++) {
memcpy(p, words[i], word_lens[i]);
p += word_lens[i];
if (i < start + wc - 1) {
*p++ = ' ';
}
}
while (p - lines[count] < maxWidth) {
*p++ = ' ';
}
*p++ = '\0';
count++;
}

*returnSize = count;
return lines;
}

int main(int argc, char **argv)
{
if (argc <= 2) {
fprintf(stderr, "Usage: ./test maxsize words...\n");
exit(-1);
}

int i, count;
char** lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);
for (i = 0; i < count; i++) {
printf("%s\n", lines[i]);
}

return 0;
}
59 changes: 33 additions & 26 deletions 076_minimum_window_substring/window_substring.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,71 @@ static char *minWindow(char *s, char *t)
{
/*
* Declare two "hash map" for ASCII chars
* f[]: represents the char found in s
* m[]: stores the chars in t
*/
* f[]: represents the char found in s
* m[]: stores the chars in t
*/
int i, f[256], m[256], pat_len = 0;
const int NOT_EXISTED = -1;
const int NOT_FOUND = 0;
memset(m, NOT_EXISTED, sizeof(m));
memset(f, NOT_EXISTED, sizeof(f));

/*
* Go through t, and inital the m[] and f[]
/*
* Go through t, and inital the m[] and f[]
* Notes: a same char can be appeared multiple times.
*/
for (i = 0; t[i] != '\0'; i++) {
m[t[i]] == NOT_EXISTED ? m[t[i]] = 1 : m[t[i]]++;
m[t[i]] == NOT_EXISTED ? m[t[i]] = 1 : m[t[i]]++;
f[t[i]] = NOT_FOUND;
pat_len++;
}

int start =-1;
int winSize = INT_MAX;
int letterFound = 0;
int size = INT_MAX;
int found = 0;
int begin = 0;
for (i = 0; s[i] != '\0'; i++) {
/* if s[i] is existed in t */
if (m[s[i]] != NOT_EXISTED){
f[s[i]]++;

/* if one char has been found enough times, then do not do letterFound++ */
if (f[s[i]] <= m[s[i]]) {
letterFound++;
/* if s[i] is existed in t */
if (m[s[i]] != NOT_EXISTED) {
/* if one char has been found enough times, then do not do found++ */
if (++f[s[i]] <= m[s[i]]) {
found++;
}
if (letterFound >= pat_len) {

if (found >= pat_len) {
/*
* Find the beginning of the window
* 1) f[s[begin]] == NOT_EXISTED ===> the char at the `begin` is not in t
* 2) f[s[begin]] > m[s[begin]] ===> a same char appeared more than excepted.
*/
while ( f[s[begin]] == NOT_EXISTED || f[s[begin]] > m[s[begin]] ) {
if ( f[s[begin]] > m[s[begin]] ) {
while (f[s[begin]] == NOT_EXISTED || f[s[begin]] > m[s[begin]]) {
if (f[s[begin]] > m[s[begin]]) {
f[s[begin]]--;
}
}
begin++;
}

int j;
printf("%d %d\n", begin, i);
for (j = begin; j < i - begin + 1; j++) {
printf("%c", s[j]);
}
printf("\n");
/* Calculate the minimized window size */
if(winSize > i - begin + 1){
if (size > i - begin + 1) {
start = begin;
winSize = i - begin + 1;
}
size = i - begin + 1;
printf("size:%d\n", size);
}
}
}
}

char *result = NULL;
if (start>=0 && winSize>0) {
result = malloc(winSize + 1);
memcpy(result, s + start, winSize);
result[winSize] = '\0';
if (start >= 0 && size > 0) {
result = malloc(size + 1);
memcpy(result, s + start, size);
result[size] = '\0';
} else {
result = malloc(1);
result[0] = '\0';
Expand Down

0 comments on commit aaa4cf0

Please sign in to comment.