MU

来源:互联网 发布:2016qq红包钓鱼源码 编辑:程序博客网 时间:2024/06/11 10:03

Description

Suppose there are the symbols M, I, and U which can be combined to produce strings of symbols called words. We start with one word MI, and transform it to get a new word. In each step, we can use one of the following transformation rules
1. Double any string after the M (that is, change Mx, to Mxx). For example MIU to MIUIU.
2. Replace any III with a U. For example MUIIIU to MUUU.
3. Remove any UU. For example MUUU to MU.
Using these three rules is it possible to change MI into a given string in a finite number of steps

Input

First line, number of strings, n. 
Following n lines, each line contains a nonempty string which consists only of letters 'M', 'I' and 'U'. 

Total length of all strings = 106. 

Output

n lines, each line is 'Yes' or 'No'. 

Sample Input

2
MI
MU

Sample Output

Yes
No

#include<string.h>#include<stdio.h>char s[1111111];int p[10]={0,1,1,0,1,0,0,0,1};int main(){    int ca,len,i,j,sum,flag;    scanf("%d",&ca);    while(ca--)    {        scanf("%s",s);        len=strlen(s);        sum=0;        if(s[0]!='M')        {            printf("No\n");            continue;        }        if(s[0]=='M'&&s[1]=='I'&&len==2)        {            printf("Yes\n");            continue;        }        flag=0;        for(i=0;i<len;i++)        {            if(s[i]=='M'&&i!=0)            {                puts("No");                flag=1;                break;            }            if(s[i]=='I')sum+=1;            else if(s[i]=='U')sum+=3;        }        if(flag==0)        {            while(sum>=9)            {                sum=(sum/9*3)+sum%9;            }            if(p[sum]==0)puts("No");            else puts("Yes");        }    }    return 0;}