hdu 4662 MU Puzzle

79 篇文章 0 订阅

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662

MU Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1296    Accepted Submission(s): 539


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 <= 10 6.
 

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

Sample Input
  
  
2 MI MU
 

Sample Output
  
  
Yes No
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4822  4821  4820  4819  4818 
 

规律题,把u全部化成i,然后多写下,就能发现规律

#include<iostream>
#include<cstring>
using namespace std;
char str[1000010];
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		cin>>str;
		int l=strlen(str),xi=0,xu=0,k=0;
		if(str[0]!='M')
		{
			cout<<"No"<<endl;
			continue;
		}
		for(int i=1;i<l;i++)
		{
			if(str[i]=='I')
			    xi++;
			else if(str[i]=='U')
			    xu++;
			else
			{
				k=1;
				break;
			}
		}
		if(k==1 || ((xi+xu)%2==1 && !(xi==1 && xu==0)) || xi%3==0)
		{
			cout<<"No"<<endl;
			continue;
		}
		else
		{
			cout<<"Yes"<<endl;
			continue;
		}	
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值