QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236602 | #7518. GCD of Pattern Matching | szlhc | WA | 0ms | 1380kb | C++14 | 2.4kb | 2023-11-04 08:58:21 | 2023-11-04 08:58:22 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
namespace io {
char ibuf[1 << 22], *ihd = ibuf, *itl = ibuf;
char getc() {
if (ihd == itl) {
ihd = ibuf;
itl = ibuf + fread(ibuf, 1, 1 << 22, stdin);
if (ihd == itl) return EOF;
}
return *(ihd++);
}
char obuf[1 << 22], *otl = obuf;
void putc(char c) {
if (otl - obuf == 1 << 22) {
fwrite(obuf, 1, 1 << 22, stdout);
otl = obuf;
}
*(otl++) = c;
}
void flush() { fwrite(obuf, 1, otl - obuf, stdout); }
}
using io::putc;
template <typename T>
void read(T &x) {
x = 0;
bool sgn = false;
char c = io::getc();
for (; c < '0' || '9' < c; c = io::getc()) sgn ^= (c == '-');
for (; '0' <= c && c <= '9'; c = io::getc()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
template <typename T>
void write(T x) {
static char stk[40];
int tp = 0;
if (x < 0) {
io::putc('-');
x = -x;
}
do {
stk[++tp] = x % 10 + '0';
x /= 10;
} while (x);
while (tp) io::putc(stk[tp--]);
}
using namespace std;
int T, n, nxt[20], cnt[30];
char s[20];
__int128 pw[20];
int main() {
scanf ("%d", &T);
while (T--) {
scanf ("%d%s", &n, s + 1);
int len = strlen(s + 1);
if (n == 2) {
__int128 x = 0;
for (int i = 1; i <= len; i++) x = x * 2 + (s[i] == s[1]);
write(x), putc('\n');
continue;
}
for (int i = 2, j = 0; i <= len; i++) {
while (j && s[j + 1] != s[i]) j = nxt[j];
if (s[j + 1] == s[i]) j++;
nxt[i] = j;
}
pw[0] = 1;
for (int i = 1; i <= len; i++) pw[i] = n * pw[i - 1];
__int128 x = 0;
if (len % (len - nxt[len]) == 0) {
for (int i = 0; i < len / (len - nxt[len]); i++) x += pw[i * (len - nxt[len])];
} else x = 1;
for (int i = 1; i <= len; i++) cnt[s[i] - 'a']++;
int num = 0, sum = 0;
for (int i = 0; i < 26; i++) {
if (cnt[i]) num++;
if (cnt[i] % (n - 1) == 1) sum++;
}
if (sum == n) x *= n - 1;
write(x), putc('\n');
for (int i = 0; i < 26; i++) cnt[i] = 0;
}
return io::flush(), 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 1376kb
input:
5 10 ccpcccpc 10 cpcpcp 10 cpc 4 cpccpc 4 dhcp
output:
10001 10101 1 65 3
result:
ok 5 number(s): "10001 10101 1 65 3"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 1380kb
input:
30 2 ab 3 abc 4 abcd 5 abcde 6 abcdef 7 abcdefg 8 abcdefgh 9 abcdefghi 10 abcdefghij 11 abcdefghijk 12 abcdefghijkl 13 abcdefghijklm 14 abcdefghijklmn 15 abcdefghijklmno 16 abcdefghijklmnop 16 a 16 ab 16 abc 16 abcd 16 abcde 16 abcdef 16 abcdefg 16 abcdefgh 16 abcdefghi 16 abcdefghij 16 abcdefghijk ...
output:
2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
wrong answer 2nd numbers differ - expected: '1', found: '2'