Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 134 Accepted Submission(s): 71
1 /* 2 * Author: kuangbin 3 * Created Time: 2013/8/8 11:54:08 4 * File Name: 1008.cpp 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring> 10 #include <cmath> 11 #include <algorithm> 12 #include <string> 13 #include <vector> 14 #include <stack> 15 #include <queue> 16 #include <set> 17 #include <time.h> 18 using namespace std; 19 char str[1000010]; 20 const int MAXN = 3000010; 21 bool dp[MAXN]; 22 bool vis[MAXN]; 23 void init() 24 { 25 memset(dp,false,sizeof(dp)); 26 memset(vis,false,sizeof(vis)); 27 dp[1] = true; 28 queue<int>q; 29 q.push(1); 30 vis[1] = true; 31 while(!q.empty()) 32 { 33 int tmp = q.front(); 34 dp[tmp] = true; 35 q.pop(); 36 if(tmp == 0){dp[0] = true;vis[0] = true;continue;} 37 if(tmp >= 6 && !vis[tmp-6]) 38 { 39 q.push(tmp-6); 40 vis[tmp-6] = true; 41 } 42 tmp *= 2; 43 while(tmp < MAXN) 44 { 45 if(vis[tmp])break; 46 dp[tmp] = true; 47 if(tmp >= 6 && !vis[tmp-6]) 48 { 49 q.push(tmp-6); 50 vis[tmp-6] = true; 51 } 52 tmp *= 2; 53 } 54 } 55 } 56 57 58 int main() 59 { 60 int T; 61 init(); 62 scanf("%d",&T); 63 while(T--) 64 { 65 bool flag = true; 66 scanf("%s",str); 67 int len = strlen(str); 68 if(str[0]!='M') 69 { 70 printf("No\n"); 71 continue; 72 } 73 int cnt = 0; 74 for(int i = 1;i < len;i++) 75 { 76 if(str[i]=='M') 77 { 78 flag = false; 79 break; 80 } 81 if(str[i] =='I')cnt++; 82 else cnt+= 3; 83 } 84 if(!flag) 85 { 86 printf("No\n"); 87 continue; 88 } 89 if(cnt == 1) 90 { 91 printf("Yes\n"); 92 continue; 93 } 94 if(cnt%2 == 1 || cnt%6 == 0)printf("No\n"); 95 else printf("Yes\n"); 96 } 97 return 0; 98 }