MU Puzzle

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

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
 

Source

 
代码写得好丑,都要丑哭了T T
U->3I, cnt/2 % 3 == 1 || cnt/2%3 == 2即可。
//2017-08-03
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
char str[N]; int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%s", str);
int cnt = ;
bool fg = ;
if (str[] == 'M')
{
for (int j = ; j < strlen(str); j++)
{
if (str[j] == 'I')
cnt++;
else if (str[j] == 'U')
cnt += ;
else if(str[j] == 'M')fg = ;
}
}else{
printf("No\n");
continue;
}
if(fg == ){
printf("No\n");
continue;
}
if (cnt <= )
{
if (cnt == || cnt == )
fg = ;
else
fg = ;
}
else
{
if (cnt % == )
fg = ;
else
{
cnt /= ;
if (cnt % == || cnt % == )
fg = ;
else
fg = ;
}
}
if (fg)
printf("Yes\n");
else
printf("No\n");
} return ;
}

最新文章

  1. Java当中的反射
  2. loadrunner --global schedule设置
  3. yii2整合百度编辑器umeditor
  4. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
  5. Html5文件
  6. POJ2823 Sliding Window
  7. json格式不对引起的报错
  8. 被Play framework狠狠的play了一把
  9. 可以获取客户端ip的API
  10. 怒刷DP之 HDU 1176
  11. MultiSelectListPreference 的使用心得
  12. ios 工程配置统一增加类的前缀(知识点也只能算知识点)
  13. python序列化之pickle
  14. 浏览器桌面通知(notifications)
  15. aix6.1 openssh安装
  16. C# 匿名方法和拉姆达表达式
  17. 《android开发艺术探索》读书笔记(一)--Activity的生命周期和启动模式
  18. 聊聊 Spring Boot 2.x 那些事儿
  19. Java:Spring @Transactional工作原理
  20. Laravel用post提交表单常见的两个错误

热门文章

  1. day02 基本数据类型与运算符
  2. 人工智能-机器学习之Selenium(chrome驱动,火狐驱动)
  3. 10-02 Java 形式参数和返回值的问题深入研究,链式编程
  4. HuLu机器学习问题与解答系列(1-8)
  5. 线程中消费者生产者的实例代码(synchronized关键字)
  6. C# 字符串操作详解
  7. Mac 安装 Ruby, Rails 运行环境
  8. 在vue中使用vuex 一个简单的实例
  9. (Dijkstra)迪杰斯特拉算法-最短路径算法
  10. PTA (Advanced Level) 1005 Spell It Right