/* Author: 2486 Memory: 0 KB Time: 31 MS Language: GNU G++ 4.9.2 Result: Accepted Public: No Yes */ //对于存在一定的极少区间段而言。能够使用前辍和进行标记,使得达到查找接近O(n) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1000+5; int T,n; int R[2][maxn],S[2][maxn],P[2][maxn]; char str[maxn]; int Compare(char x,char y) { if(x=='P'&&y=='R'||x=='S'&&y=='P'||x=='R'&&y=='S') { return 1; } if(x==y)return 2; return 0; } int main() { //freopen("D://imput.txt","r",stdin); scanf("%d",&T); while(T--) { memset(R,0,sizeof(R)); memset(S,0,sizeof(S)); memset(P,0,sizeof(P)); scanf("%d%s",&n,str); for(int i=0; i<n; i++) { R[0][i+1]=R[0][i]; R[1][i+1]=R[1][i]; P[0][i+1]=P[0][i]; P[1][i+1]=P[1][i]; S[0][i+1]=S[0][i]; S[1][i+1]=S[1][i]; if(Compare('R',str[i])==1) { R[0][i+1]=R[0][i]+1; } else if(Compare('R',str[i])==0) { R[1][i+1]=R[1][i]+1; } if(Compare('P',str[i])==1) { P[0][i+1]=P[0][i]+1; } else if(Compare('P',str[i])==0){ P[1][i+1]=P[1][i]+1; } if(Compare('S',str[i])==1) { S[0][i+1]=S[0][i]+1; } else if(Compare('S',str[i])==0){ S[1][i+1]=S[1][i]+1; } } int cnt=0; for(int i=0; i<=n; i++) { for(int j=i; j<=n; j++) { if(R[0][i]+P[0][j]-P[0][i]+S[0][n]-S[0][j]-R[1][i]-(P[1][j]-P[1][i])-(S[1][n]-S[1][j])>0) { cnt++; //printf("[%d,%d] ",i,j); } } } printf("%d ",cnt); } return 0; }