/*author:2486memory:0 kbtime:31 mslanguage:gnu g++ 4.9.2result:acceptedpublic:no yes*///for the presence of a certain few interval segments, you can use the prefix and tag, Makes reaching find near 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 ' &&am 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Rock-paper-scissors