Atcoder ABC162 D - RGB Triplets

传送门:D - RGB Triplets

 题意:给你一个只含‘R’,‘G’,‘B’的字符串,求有多少个长度为3且每个字符都不相等,并且第一第二和第二第三的区间长度不同的子序列.

题解:统计每个字符各有多少,算出所有两两不同的子序列个数然后减去区间长度相等的个数即可

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <unordered_set>
12 #include <unordered_map>
13 #define ll long long
14 #define fi first
15 #define se second
16 #define pb push_back
17 #define me memset
18 const int N = 1e6 + 10;
19 const int mod = 1e9 + 7;
20 using namespace std;
21 typedef pair<int,int> PII;
22 typedef pair<long,long> PLL;
23  
24 int n;
25 ll cnt=0;
26 ll r,g,b;
27 string s;
28  
29 int main() {
30     ios::sync_with_stdio(false);
31     cin>>n>>s;
32    for(int i=0;i<n;++i){
33        for(int j=i+1;j<n;++j){
34            int k=j+(j-i);
35            if(k>n-1) continue;
36            if(s[i]!=s[j] && s[j]!=s[k] && s[i]!=s[k]) cnt++;
37        }
38    }
39  
40    for(int i=0;i<n;++i){
41        if(s[i]=='R') r++;
42        else if(s[i]=='G') g++;
43        else b++;
44    }
45    printf("%lld\n",r*g*b-cnt);
46  
47  
48     return 0;
49 }
View Code


 

猜你喜欢

转载自www.cnblogs.com/lr599909928/p/12688958.html