74 lines
1.4 KiB
C
74 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct ascii_set {
|
|
unsigned long long lo;
|
|
unsigned long long hi;
|
|
};
|
|
|
|
bool ascii_set_insert(struct ascii_set *set, char ch)
|
|
{
|
|
unsigned long long mask = 1ULL << (ch % 64);
|
|
|
|
if (ch < 64) {
|
|
if (set->lo & mask) {
|
|
set->lo = mask;
|
|
set->hi = 0;
|
|
return false;
|
|
} else {
|
|
set->lo |= mask;
|
|
return true;
|
|
}
|
|
} else {
|
|
if (set->hi & mask) {
|
|
set->hi = mask;
|
|
set->lo = 0;
|
|
return false;
|
|
} else {
|
|
set->hi |= mask;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
unsigned longest_no_repeat(char *str, unsigned len)
|
|
{
|
|
struct ascii_set set = {0};
|
|
|
|
unsigned longest = 0;
|
|
unsigned current = 0;
|
|
|
|
for (unsigned i = 0; i < len; i++) {
|
|
if (ascii_set_insert(&set, str[i])) {
|
|
current++;
|
|
} else {
|
|
if (current > longest)
|
|
longest = current;
|
|
|
|
current = 1;
|
|
}
|
|
}
|
|
|
|
if (current > longest)
|
|
longest = current;
|
|
|
|
return longest;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
char buf[2048];
|
|
|
|
while (true) {
|
|
fputs("enter a string nya: ", stdout);
|
|
fgets(buf, 2048, stdin);
|
|
unsigned len = strlen(buf);
|
|
buf[--len] = '\0';
|
|
|
|
unsigned longest = longest_no_repeat(buf, len);
|
|
printf("longest without repeating is %u nya\n", longest);
|
|
}
|
|
|
|
return 0;
|
|
} |