Section 2.1
parent
a88cc99983
commit
6da49be118
@ -0,0 +1,23 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
char s[1000010];
|
||||
int next[1000010], n, k, len;
|
||||
int main()
|
||||
{
|
||||
scanf("%d%d%s", &n, &k, s);
|
||||
next[0] = -1;
|
||||
len = strlen(s);
|
||||
for (int i = 1; i < len; ++i)
|
||||
{
|
||||
int j;
|
||||
for (j = next[i - 1]; j != -1 && s[j + 1] != s[i]; j = next[j]);
|
||||
if (s[j + 1] == s[i]) j++;
|
||||
next[i] = j;
|
||||
}
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
int p = i + 1, q = p / (i - next[i]);
|
||||
putchar(((p % (i - next[i]) == 0) ? (q / k >= q % k ? '1' : '0') : (q / k > q % k ? '1' : '0')) );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
for (int i = 0, j = -1; i < len; i++)
|
||||
{
|
||||
while (~j && t[j + 1] != s[i]) j = next[j];
|
||||
if (t[j + 1] == s[i]) j++;
|
||||
if (j == len - 1) ans++, j = next[j];
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
for (int i = 1, j = -1; i < len; i++)
|
||||
{
|
||||
while (~j && str[j + 1] != str[i]) j = next[j];
|
||||
if (str[j + 1] == str[i]) j++;
|
||||
next[i] = j;
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
char str[1 << 20 | 1];
|
||||
int next[1 << 20 | 1];
|
||||
int len;
|
||||
int main()
|
||||
{
|
||||
next[0] = -1;
|
||||
while (scanf("%s", str))
|
||||
{
|
||||
if (str[0] == '.') break;
|
||||
len = strlen(str);
|
||||
for (int i = 0, j = -1; i < len;)
|
||||
(~j && str[i] != str[j]) ? j = next[j] : next[++i] = ++j;
|
||||
printf("%d\n", len % (len - next[len]) == 0 ? len / (len - next[len]) : 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct node
|
||||
{
|
||||
node *trans[4];
|
||||
int cnt;
|
||||
} nodes[400010];
|
||||
int tot;
|
||||
node *root;
|
||||
inline node *new_node() { return &nodes[tot++]; }
|
||||
void try_insert(node *n, char *str)
|
||||
{
|
||||
if (*str == '\0')
|
||||
n->cnt++;
|
||||
else
|
||||
{
|
||||
if (n->trans[*str - '0'] == 0) n->trans[*str - '0'] = new_node();
|
||||
try_insert(n->trans[*str - '0'], str + 1);
|
||||
}
|
||||
}
|
||||
char f[1 << 8 | 1];
|
||||
int ans[20010];
|
||||
int main()
|
||||
{
|
||||
f['A'] = '0', f['C'] = '1', f['G'] = '2', f['T'] = '3';
|
||||
int n, m;
|
||||
char buf[22];
|
||||
while (~scanf("%d%d", &n, &m) && (n + m))
|
||||
{
|
||||
memset(ans, 0, sizeof(ans));
|
||||
memset(nodes, 0, sizeof(nodes));
|
||||
tot = 0;
|
||||
root = new_node();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
scanf("%s", buf);
|
||||
for (int j = 0; j < m; j++)
|
||||
buf[j] = f[buf[j]];
|
||||
try_insert(root, buf);
|
||||
}
|
||||
for (int i = 0; i < tot; i++) ans[nodes[i].cnt]++;
|
||||
for (int i = 1; i <= n; i++)
|
||||
printf("%d\n", ans[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
char a[1 << 20 | 1], b[1 << 14 | 1];
|
||||
int la, lb;
|
||||
int next[1 << 14 | 1];
|
||||
int main()
|
||||
{
|
||||
next[0] = -1;
|
||||
int n;
|
||||
scanf("%d", &n);
|
||||
while (n--)
|
||||
{
|
||||
scanf("%s%s", b, a);
|
||||
la = strlen(a), lb = strlen(b);
|
||||
for (int i = 1, j = -1; i < lb; i++)
|
||||
{
|
||||
while (~j && b[j + 1] != b[i]) j = next[j];
|
||||
if (b[j + 1] == b[i]) j++;
|
||||
next[i] = j;
|
||||
}
|
||||
int ans = 0;
|
||||
for (int i = 0, j = -1; i < la; i++)
|
||||
{
|
||||
while (~j && b[j + 1] != a[i]) j = next[j];
|
||||
if (b[j + 1] == a[i]) j++;
|
||||
if (j == lb - 1) ans++, j = next[j];
|
||||
}
|
||||
printf("%d\n", ans);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct node
|
||||
{
|
||||
node *trans[10];
|
||||
bool is_end;
|
||||
} nodes[100010];
|
||||
node *root;
|
||||
int cnt;
|
||||
node *new_node() { return &nodes[cnt++]; }
|
||||
char buf[11];
|
||||
bool try_insert(node *n, char *str)
|
||||
{
|
||||
if (n->is_end) return false;
|
||||
if (*str == '\0')
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
if (n->trans[i])
|
||||
return false;
|
||||
n->is_end = true;
|
||||
return true;
|
||||
}
|
||||
if (n->trans[*str - '0'] == 0) n->trans[*str - '0'] = new_node();
|
||||
return try_insert(n->trans[*str - '0'], str + 1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int t, n;
|
||||
scanf("%d", &t);
|
||||
while (t--)
|
||||
{
|
||||
memset(nodes, 0, sizeof(nodes));
|
||||
cnt = 0;
|
||||
root = new_node();
|
||||
scanf("%d", &n);
|
||||
bool flag = true;
|
||||
while (n--)
|
||||
{
|
||||
scanf("%s", buf);
|
||||
if (flag) flag = try_insert(root, buf);
|
||||
}
|
||||
puts(flag ? "YES" : "NO");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
struct node
|
||||
{
|
||||
node *trans[26];
|
||||
int cnt;
|
||||
};
|
||||
void insert(node *n, char *str)
|
||||
{
|
||||
for (; *str; n = n->trans[*str - '0'])
|
||||
if (n->trans[*str - '0'] == 0)
|
||||
n->trans[*str - '0'] = new_node();
|
||||
n->cnt++;
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in new issue