HDU 4662 MU Puzzle

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

首先需要转换一下题意:

1.首先有且只有1个M而且在开头才符合题意。

2.U其实完全可以等价成是3个I,所以对于当前i个的个数,我们可以*2,或者是-6.

因此我们可以统计出串中I的个数,U算3个,I算一个。然后看当前的I能不能通过1的转换(*2或者-6)变出来即可。

哈哈哈,打表大法好,直接看完全看不出来呀,打个表就好啦<(=o゜▽゜)o☆

可以看出来除了1是个奇数外,其余的都是偶数,然后缺少6的倍数。

OK了~

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;
const int MAXN = 1e6+7;
int n;
char s[MAXN];
//这是打表的代码
bool vis[MAXN];
int ans[MAXN];
void bfs()
{
    queue<int>q;
    q.push(1);
    int cnt = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        ans[cnt++] = u;
        if(cnt >= 1000)break;
        if(u >= 6)if(!vis[u-6])
        {
            vis[u-6] = 1;
            q.push(u-6);
        }
        if(u*2<=1000000&&!vis[u*2])
        {
            vis[u*2] = 1;
            q.push(u*2);
        }
    }
    printf("%d\n",cnt);
    sort(ans,ans+cnt);
    for(int i = 0; i < cnt; ++i)
    {
        printf("%d ",ans[i]);
        if(i%10 == 1)puts("");
    }
}
//

int main()
{
    //bfs();
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s);
        int l = strlen(s);
        int sum = 0;
        int flag = 1;
        if(s[0] != 'M')flag = 0;
        if(flag)
            for(int i = 1; i < l; ++i)
            {
                if(s[i] == 'M')
                {
                    flag = 0;
                    break;
                }
                else if(s[i] == 'I')sum++;
                else sum += 3;
            }
        if(!flag)puts("No");
        else
        {
            if(sum == 1)puts("Yes");
            else
            {
                if(sum & 1 || sum%6==0)puts("No");
                else puts("Yes");
            }
        }
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值