hdu 4662 MU Puzzle

来源:互联网 发布:linux cdn 编辑:程序博客网 时间:2024/04/29 14:15
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
2MIMU

Sample Output
YesNo
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;}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 健康证和培训证怎么办 楼下烧煤炉呛人怎么办 衣服染了笔渍怎么办 济宁二院怎么办健康证 刚按揭车的想卖怎么办 房子已过户银行不放贷怎么办 办房贷信用逾期给拒了怎么办 二手房过户了房贷下不来怎么办 没有房证的房子买卖怎么办 老公失踪房产证要他签字怎么办 税务机关对长期欠税怎么办 离婚有贷款的房子怎么办 买房没过户房主反悔怎么办 房子过户卖方爱人去世怎么办 黄浦区学生社会实践护照掉了怎么办 北京医保卡坏了怎么办 去了越南的签证怎么办 护照签证到期还在国外怎么办 办护照预约满了怎么办 未满16岁怎么办护照 办理护照采集指纹录不上怎么办 办理护照时指纹没有怎么办 未满16岁的怎么办护照 身份证照得太丑怎么办 武汉江夏怎么办健康证 小孩偏胖肚子大怎么办 手机安全中心密码忘记了怎么办 进京证一年12次怎么办 青霉素和头孢都过敏怎么办 手汗孢疹肿胀怎么办能吃消炎药吗 手机上办进京证怎么办 杭州公共自行车还车点满了怎么办 法院判决书下来对方不给钱怎么办 抓进看守所的人怎么办 在拘留所里病重抢救怎么办 昆明公租房户型确认后怎么办 18年工作好难找怎么办 我没工作了怎么办2018 婚姻质量差不幸福该怎么办 吸毒犯戒毒所不收怎么办 信用卡到期人进拘留所怎么办?