hdu 4662 MU Puzzle

Problem 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
1、Mx -> Mxx
2、III -> U
3、UU -> 空
问MI 能否进过上面操作转化成  读入的字符串。
重点是怎么推式子
1、M前面的不能有字母。
2、只能有一个M
3、操作一只能得到 2^n个I (n > 0)
4、可以消去6个I.
然后得到一个公式  2^n - 6 * m = sum; (sum 表示读入的字符串中I的个数);
#include<iostream>
#include<cstring>
using namespace std;
char s[1000001];
int main()
{
    int n,j,u,i;
    cin>>n;
    while(n--)
    {
        cin>>s;
        u=i=0;
        if(s[0]!='M'||s[1]==0)
        {
            cout<<"No"<<endl;

        }
        else if(strcmp(s,"MI")==0)
        {
            cout<<"Yes"<<endl;
        }
        else if(strcmp(s,"MU")==0)
        {
            cout<<"No"<<endl;
        }
        else
        {
            for(j=1;s[j];j++)
            {
                if(s[j]=='U')
                u++;
                else if(s[j]=='I')
                i++;
                if(s[j]=='M')
                {
                    cout<<"No"<<endl;
                    break;
                }
            }
            if(s[j])
            {
                continue;
            }
            if((u*3+i)%2==0&&i>0&&(u*3+i)%3!=0)
            cout<<"Yes"<<endl;
            else
            cout<<"No"<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值