Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Longest-string-challenge/C/longest-string-challenge-1.c
Normal file
28
Task/Longest-string-challenge/C/longest-string-challenge-1.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int cmp(const char *p, const char *q)
|
||||
{
|
||||
while (*p && *q) p = &p[1], q = &q[1];
|
||||
return *p;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char line[65536];
|
||||
char buf[1000000] = {0};
|
||||
char *last = buf;
|
||||
char *next = buf;
|
||||
|
||||
while (gets(line)) {
|
||||
strcat(line, "\n");
|
||||
if (cmp(last, line)) continue;
|
||||
if (cmp(line, last)) next = buf;
|
||||
last = next;
|
||||
strcpy(next, line);
|
||||
while (*next) next = &next[1];
|
||||
}
|
||||
|
||||
printf("%s", buf);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
% printf "a\nbb\nccc\nddd\nee\nf\nggg" | ./a.out
|
||||
ccc
|
||||
ddd
|
||||
ggg
|
||||
83
Task/Longest-string-challenge/C/longest-string-challenge-3.c
Normal file
83
Task/Longest-string-challenge/C/longest-string-challenge-3.c
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int inc(int x) { return (int)&((char *)x)[1]; }
|
||||
int dec(int x) { return (int)&((char *)x)[-1]; }
|
||||
int gt(int x, int y)
|
||||
{
|
||||
while (y && x) y = dec(y), x = dec(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int eq(int x, int y)
|
||||
{
|
||||
return !gt(x, y) && !gt(y, x);
|
||||
}
|
||||
|
||||
int add(int x, int y)
|
||||
{
|
||||
while(y) x = inc(x), y = dec(y);
|
||||
return x;
|
||||
}
|
||||
|
||||
/* strlen(a) + 1 */
|
||||
int length(const char *a)
|
||||
{
|
||||
char *x = 0; // assuming (int)(char*)0 == 0
|
||||
if (!a) return 0;
|
||||
while (*a) a++, x++;
|
||||
return (int)x;
|
||||
}
|
||||
|
||||
char *str_cat(char *a, const char *b)
|
||||
{
|
||||
int len = add(1, add(length(a), length(b)));
|
||||
if (!(a = realloc(a, len))) abort();
|
||||
return strcat(a, b);
|
||||
}
|
||||
|
||||
char *get_line(char *l, FILE *fp)
|
||||
{
|
||||
int c, len = 0;
|
||||
char tmp[2] = {0};
|
||||
|
||||
*l = 0;
|
||||
while ((c = fgetc(fp)) != EOF) {
|
||||
*tmp = c;
|
||||
len = inc(len);
|
||||
|
||||
l = str_cat(l, tmp);
|
||||
if (eq(*tmp, '\n')) return l;
|
||||
}
|
||||
|
||||
*tmp = '\n';
|
||||
return len ? str_cat(l, tmp) : l;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int l1, l2;
|
||||
char *line = malloc(1), *buf = malloc(1), *longest = malloc(1);
|
||||
while (1) {
|
||||
line = get_line(line, stdin);
|
||||
|
||||
if (!(l1 = length(line))) break;
|
||||
l2 = length(longest);
|
||||
|
||||
if (gt(l1, l2)) {
|
||||
*buf = *longest = 0;
|
||||
longest = str_cat(longest, line);
|
||||
} else if (gt(l2, l1)) continue;
|
||||
|
||||
buf = str_cat(buf, line);
|
||||
}
|
||||
printf("%s", buf);
|
||||
|
||||
free(buf);
|
||||
free(longest);
|
||||
free(line);
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
Task/Longest-string-challenge/C/longest-string-challenge-4.c
Normal file
33
Task/Longest-string-challenge/C/longest-string-challenge-4.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int longer(const char *p, const char *q) {
|
||||
while (*p && *q) p = &p[1], q = &q[1];
|
||||
return *p;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char line[100000];
|
||||
char buf[1100001];
|
||||
char *linend= &line[99999];
|
||||
char *bufend= &buf[1000000];
|
||||
char *last = buf;
|
||||
char *next = buf;
|
||||
|
||||
memset(line, 1, 100000);
|
||||
memset(buf, 1, 1100001);
|
||||
buf[0]= buf[1100000]= 0;
|
||||
while (fgets(line, 100000, stdin)) {
|
||||
if (!*linend) exit(1);
|
||||
if (longer(last, line)) continue;
|
||||
if (!longer(bufend, line)) exit(1);
|
||||
if (longer(line, last)) next = buf;
|
||||
last = next;
|
||||
strcpy(next, line);
|
||||
while (*next) next = &next[1];
|
||||
}
|
||||
|
||||
printf("%s", buf);
|
||||
exit(0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue