QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#238174#7518. GCD of Pattern Matchingpiggy123WA 1ms3468kbC++143.3kb2023-11-04 15:58:362023-11-04 15:58:37

Judging History

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

  • [2023-11-04 15:58:37]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3468kb
  • [2023-11-04 15:58:36]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

ll p[55];

int main(){
	ll t;
	cin >> t;
	while (t--){
		ll m;
		string s;
		cin >>m>> s;
		if (m==2){
			ll pw=0;
			for (ll i=0;i<s.length();i++){
				if (s[i]==s[0])pw*=2,pw++;
				else pw*=2;
			}
			cout<<pw<<"\n";
			continue;
		}
		for (ll j=0;j<26;j++)p[j]=0;
		set<char> st;
		for (ll i=0;i<s.length();i++){
			st.insert(s[i]);
			for (ll j=0;j<26;j++){
				if (s[i]-'a'!=j){
					p[j]*=m;
				}else{
					p[j]*=m;
					p[j]++;
				}
			}
		}
		ll ans=0;
		map<char,ll> mp;
		ll nw=m-1;
		for (ll i=0;i<s.length();i++){
			if (mp[s[i]]){
				ans*=m,ans+=mp[s[i]]-1;
			}else{
				mp[s[i]]=nw+1;
				nw--;
				ans*=m,ans+=mp[s[i]]-1;
			}
		}
		if (st.size()!=m)
		for (ll j=0;j<26;j++)if (p[j])ans=__gcd(ans,p[j]);
		for (ll i=0;i<26;i++){
			for (ll j=i+1;j<26;j++){
				if (p[i]&&p[j]){
					ans=__gcd(ans,abs(p[i]-p[j]));
				}
			}
		}
		cout<< ans<<"\n";
	}
	return 0;
}

/*
                                                                
 ■■■■■     ■■      ■■■     ■■■   ■    ■     ■     ■■■■    ■■■■  
 ■   ■■    ■■     ■  ■■   ■  ■■  ■    ■    ■■     ■  ■■  ■■  ■  
 ■    ■    ■■    ■    ■  ■    ■   ■  ■    ■■■    ■■  ■■  ■   ■■ 
 ■    ■    ■■    ■    ■  ■    ■   ■  ■     ■■    ■   ■■      ■■ 
 ■    ■    ■■    ■       ■         ■■      ■■        ■■      ■  
 ■   ■■    ■■    ■  ■■■  ■  ■■■    ■■      ■■       ■■     ■■■  
 ■■■■■     ■■    ■    ■  ■    ■    ■■      ■■      ■■        ■■ 
 ■         ■■    ■    ■  ■    ■    ■■      ■■     ■■          ■ 
 ■         ■■    ■    ■  ■    ■    ■■      ■■     ■      ■   ■■ 
 ■         ■■    ■■  ■■  ■■  ■■    ■■      ■■    ■       ■■  ■■ 
 ■         ■■      ■■■■    ■■■■    ■■      ■■    ■■■■■■   ■■■■  
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3468kb

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
1
3
2
5
3
7
4
9
5
11
6
13
7
-1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

result:

wrong answer 15th numbers differ - expected: '15', found: '-1'