MU Puzzle HDU - 4662

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?

InputFirst 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.Outputn lines, each line is ‘Yes‘ or ‘No‘.Sample Input

2
MI
MU

Sample Output

Yes
No

所有U都是由I换来的,而U不能再换成I,所以可以讲所有U换成I的数目进行统计,如果符合要求即可1.相当于I的数目*23.相当于I的数目-6
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 1000000 + 10;
char s[maxn];
int p[30];

void init(){
    p[0] = 1;
    for(int i = 1; i < 30; i++) p[i] = (p[i-1] << 1);
}

bool solve(){
    bool ok = 0;
    int len = strlen(s), i, Mcnt = 0, Icnt = 0;
    for(i = 0; i < len; i++){
        if(s[i] == ‘M‘) Mcnt++;
        else if(s[i] == ‘I‘) Icnt++;
        else if(s[i] == ‘U‘) Icnt += 3;
    }
    if(Mcnt == 1 && s[0] == ‘M‘){
        for(i = 29; i >= 0; i--) if(p[i] >= Icnt && (p[i] - Icnt) % 6 == 0) {
            ok = 1;
            break;
        }
    }
    return ok;
}

int main()
{
    int n;
    init();
    scanf("%d", &n);
    getchar();
    while(n--){
        scanf("%s", s);
        if(solve()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
时间: 2024-10-29 19:13:43

MU Puzzle HDU - 4662的相关文章

hdu 4662 MU Puzzle

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 MU PuzzleTime 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

HDU 4662 MU Puzzle:找规律

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 题意: 初始字符串为"MI". 有三个操作: (1)将'M'之后的所有字符翻倍.For example: MIU to MIUIU. (2)将'III'变为一个'U'.For example: MUIIIU to MUUU. (3)删除'UU'.For example: MUUU to MU 给你一个字符串s,问你是否能将初始字符串"MI"通过一系列操作变为s.

MU puzzle

2017-08-06 20:49:38 writer:pprp 三种操作: 1.MUI -> MUIUI 2.MUUU -> MU 3.MUIII -> MUU 分析:有两个操作:将所有的U都换成I对I的个数进行判断: 1的操作是将这个个数乘以2 2/3操作综合起来相当于可以-6 于是可以计算出来I的个数,判断能否通过*2或者-6的操作将其变成1 代码如下; #include<cstdio> #include<cstring> #include<algori

[2016-02-05][HDU][1097][A hard puzzle]

[2016-02-05][HDU][1097][A hard puzzle] HDU - 1097 A hard puzzle Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know

2019 杭电多校 第四场

2019 Multi-University Training Contest 4 补题链接:2019 Multi-University Training Contest 4 1001 AND Minimum Spanning Tree (HDU 6614) 题意 给定一个有 \(N\) 个结点的完全图,编号从 \(1\) 到 \(N\).结点 \(x\) 与结点 \(y\) \((1\leq x, y\leq N, x \neq y)\) 的边的权值为 \(x\) 与 \(y\) 按位与的值,求

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5465 Clarke and puzzle Accepts: 42 Submissions: 269 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一

HDU 1098 Ignatius&#39;s puzzle 费马小定理+扩展欧几里德算法

题目大意: 给定k,找到一个满足的a使任意的x都满足 f(x)=5*x^13+13*x^5+k*a*x 被65整除 推证: f(x) = (5*x^12 + 13 * x^4 + ak) * x 因为x可以任意取 那么不能总是满足 65|x 那么必须是 65 | (5*x^12 + 13 * x^4 + ak) 那么就是说 x^12 / 13 + x^4 / 5 + ak / 65 正好是一个整数 假设能找到满足的a , 那么将 ak / 65 分进x^12 / 13 + x^4 / 5中得到

HDU 1098 Ignatius&#39;s puzzle 也不大懂

http://acm.hdu.edu.cn/showproblem.php?pid=1098 看了一下它们的思路,没完全明白,但是能写出来,大概可能也许就是这样做的吧. 1 #include <iostream> 2 using namespace std; 3 typedef long long ll; 4 5 int main() { 6 ll k; 7 while (cin>>k) 8 { 9 int flag = 0,a; 10 for ( a = 1; a <= 6

HDU - 1098 - Ignatius&#39;s puzzle (数论 - 费马小定理)

Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7012    Accepted Submission(s): 4847 Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no