### 藍橋杯-算法提高-Cowboys

算法訓練 Cowboys

時間限制：2.0s   內存限制：256.0MB

P.s：“A”表示順時針方向，“B”表示逆時針方向。當一對相鄰的牛仔發現他們正在互指的時候，就會轉過身。說明將‘BA’轉化為‘AB’；然而 牛仔環“ABBBABBBA”在一秒后會變成“BABBBABBA”；而牛仔環“BABBA”會變成“ABABB” 說明是將‘AB’轉化為‘BA’；以給的例子為准。（樣例似乎兩者都可）

`#include <queue>#include <functional>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <stack>#include <vector>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#include <assert.h>#define REP(i,k,n) for(int i=k;i<n;i++)#define REPP(i,k,n) for(int i=k;i<=n;i++)#define scan(d) scanf("%d",&d)#define scann(n,m) scanf("%d%d",&n,&m)#define mst(a,k)  memset(a,k,sizeof(a));#define LL long long#define eps 1e-8#define INF 0x3f3f3f3f#define mod 1000000007#define PI acos(-1.)using namespace std;#define N 105#define M 10005int dp[N][2];string s;int main(){   cin>>s;   bool flag = 0;   int len = s.length();   for(int i=1;i<len;i++){      if(s[i]!=s[0]){         flag = 1;         break;      }   }   if(!flag){                     //如果都為 ‘A’或 ‘B’      cout<<1; return 0;   }   int cot;   for(int i=0;i<len;i++){        //找到 ‘BA’       if(s[ (i+1) %len] == s[i] - 1){         cot = i; break;      }   }   int st = (cot + 2) % len;   int ed = (cot - 1 + len) % len;   dp[st][0] = 1;   dp[st][1] = 0;   while(st != ed){      st = (st+1) % len;      if(s[st]==s[(st-1+len) %len]){         dp[st][0] = dp[ (st-1+len) %len][0] + dp[(st-1+len) %len][1];         dp[st][1] = 0;      }else if(s[st] == s[(st-1+len) %len] + 1){       dp[st][0] = dp[(st-1+len) %len][1];       dp[st][1] = 0;      }else{       dp[st][0] = dp[(st-1+len) %len][0] + dp[(st-1+len) %len][1];       dp[st][1] = dp[(st-2+len) %len][0] + dp[(st-2+len) %len][1];       if(dp[st][1] == 0) dp[st][1] = 1;      }   }   int sum = dp[st][0] + dp[st][1];    if(s[(cot + 2) % len]=='B' && s[(cot + 3) % len]=='B'){      cout<<sum; return 0;                 //如果是 BABB   }   if(s[(cot - 1 + len) % len]=='A' && s[(cot -2 + len) % len]=='A'){      cout<<sum; return 0;                 // 如果是 AABA   }   int length = 2;   st = (cot + 2) % len;   ed = (cot - 1 + len) % len;   if(s[(cot + 2) % len]=='B' && s[(cot + 3) % len]=='A'){      st = (st + 2) % len;      length +=2;   }    if(s[(cot - 1 + len) % len]=='A' && s[(cot -2 + len) % len]=='B'){      ed =(ed - 2 + len) % len;      length+=2;   }   if(length>=len){              //如果 是 BABA 或者 BABABA       cout<<sum+1; return 0;   }   memset(dp,0,sizeof(dp));      //注意清空   dp[st][0] = 1;   dp[st][1] = 0;    while(st != ed){      st = (st+1) % len;      if(s[st]==s[(st-1+len) %len]){         dp[st][0] = dp[ (st-1+len) %len][0] + dp[(st-1+len) %len][1];         dp[st][1] = 0;      }else if(s[st] == s[(st-1+len) %len] + 1){       dp[st][0] = dp[(st-1+len) %len][1];       dp[st][1] = 0;      }else{       dp[st][0] = dp[(st-1+len) %len][0] + dp[(st-1+len) %len][1];       dp[st][1] = dp[(st-2+len) %len][0] + dp[(st-2+len) %len][1];       if(dp[st][1] == 0) dp[st][1] = 1;      }   }   sum += dp[st][0] + dp[st][1];   cout<<sum;   return 0;}`