QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#551109#7518. GCD of Pattern Matchingretired_midlightsWA 0ms3812kbC++141.2kb2024-09-07 15:29:562024-09-07 15:29:56

Judging History

你现在查看的是最新测评结果

  • [2024-09-07 15:29:56]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3812kb
  • [2024-09-07 15:29:56]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int T;
int m;
string s;

const int MAXN = 35;
int cnt, tmp[MAXN];
long long a[MAXN];

long long llgcd(long long a,long long b) {
    return b==0 ? a : llgcd(b, a%b);
}
long long llabs(long long a) {
    return a<0 ? -a : a;
}

int main() {
    cin >> T;
    while(T--) {
        cin >> m >> s;
        cnt = 0;
        for( int i=0; i<26; i++) {
            tmp[i] = 0;
            a[i] = 0;
        }
        long long w = 1;
        for(int i = s.length()-1; i>=0; i--) {
            if(!tmp[s[i]-'a']) {
                cnt++;
                tmp[s[i]-'a'] = cnt;
            }
            int k = tmp[s[i]-'a'];
            a[k] += w;
            w *= m;
        }
        sort(a+1, a+cnt+1);

        long long ans = 0;
        if( cnt < m ) {
            for(int k=1; k<=cnt; k++) {
                ans = llgcd(a[k], ans);
            }
        }
        else {
            ans = 0;
            for(int k=cnt; k>=1; k--) {
                ans += a[k] * (cnt-k);
            }
            for(int k=1; k<cnt; k++) {
                ans = llgcd(a[k+1]-a[k], ans);
            }
        }
        printf("%lld\n", ans);
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3796kb

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: 3812kb

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:

1
1
3
2
5
3
7
4
9
5
11
6
13
7
15
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

wrong answer 1st numbers differ - expected: '2', found: '1'