Rock-Paper-Scissors
2017-07-31 17:51 tlnshuju 阅读(180) 评论(0) 编辑 收藏 举报
/* 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]\n",i,j); } } } printf("%d\n",cnt); } return 0; }