HDU 4662 MU Puzzle

MU Puzzle

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


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
 

Recommend
zhuyuanchen520
 
題意:  
1、Mx -> Mxx
2、III -> U
3、UU -> 空
問MI 能否進過上面操作轉化成  讀入的字符串。

思路:模擬
根據3個操作可知:
1、M前面的不能有字母。
2、只能有一個M
3、操作一隻能得到 2^n個I (n > 0)
4、可以消去6個I.
然後得到一個公式  2^n - 6 * m = sum; (sum 表示讀入的字符串中I的個數);

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

//

const int V = 1000000 + 50;
const int MaxN = 80 + 5;
const int mod = 10000 + 7;
const __int64 INF = 0x7FFFFFFFFFFFFFFFLL;
const int inf = 0x7fffffff;
int n, flag;
char ch[V];
int main() {
    int i, j;
    scanf("%d", &n);
    while(n--) {
        flag = 1;
        scanf("%s", &ch);
        if(ch[0] != 'M')
            printf("No\n");
        else{
            int sum = 0;
            for(i = 1; ch[i]; ++i) {
                if(ch[i] == 'M') {
                    flag = 0;
                    break;
                }
                else if(ch[i] == 'U')
                    sum += 3;
                else
                    sum ++;
            }
            if(!flag) {
                printf("No\n");
                continue;
            }
            if(sum == 1) {
                printf("Yes\n");
                continue;
            }
            if(sum % 2 == 1) {
                printf("No\n");
                continue;
            }
            for(i = 25; i >= 0; --i) {
                if(((1 << i) - sum % 6) == 0) {
                    flag = 0;
                    break;
                }
            }
            if(flag)
                printf("No\n");
            else
                printf("Yes\n");
        }
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章