其他分享
首页 > 其他分享> > Day 4 K - Expression in Memories HDU 6342 | 模拟

Day 4 K - Expression in Memories HDU 6342 | 模拟

作者:互联网

一开始想的很乱,后来发现如果先把?处理了可以大大减少讨论的难度。很多地方还是要多试几组自己想的输入数据才能发现问题……别莽交。

?0??+?00?0*??0??00?00??00?0??0??0

??0??2?0?3?0?3?0?00?30??0?3?000?20

?0??0?0+?0?000*??0?2?00+4?0+2?0000

?0?0+?*?0??3?+3?3

0???0+0??00000???000

?0+0??+0??000+?0?000000?+??0000?0?0?+?0?0?

?0000???000??00???0000+20???0000*30??000

?0000?+??+??000?+?00???0000+20??+??0000*30??000

?0+0?0?10?+?0+0??

#include <bits/stdc++.h>

using namespace std;

char s[600];
int T, len;

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", s);

        len = strlen(s);

        for(int i = 0;i < len;i++)
        {
            if(s[i] == '?')
            {
                if(s[i-1] == '0' && (s[i+1] >= '0' && s[i+1] <= '9'))
                {
                    if(s[i-2] >= '0' && s[i-2] <= '9')
                        s[i] = '1';
                    else
                        s[i] = '+';
                }

                else if(s[i-1] == '0' && s[i+1] == '?')
                {
                    if(s[i-2] >= '0' && s[i-2] <= '9')
                        s[i] = '1';
                    else
                        s[i] = '+';
                }
                else
                    s[i] = '1';
            }

            //printf("%c", s[i]);
        }
        //printf("\n");


        /**第一位和最后一位不可以是运算符**/
        if(s[0] == '+' || s[0] == '*')
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        if(s[len-1] == '+' || s[len-1] == '*')
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        if(s[0] == '0' && s[1] <= '9' && s[1] >= '0')
        {
            printf("IMPOSSIBLE\n");
            continue;
        }


        int wa = 0;
        for(int i = 0;i < len;i++)
        {
             /**前导零不合法**/
            if(s[i] == '0' && s[i+1] <= '9' && s[i+1] >= '0' && (s[i-1] == '+' || s[i-1] == '*'))
            {
                wa = 1;
                break;
            }

            /**字符串连续不合法**/
            if((s[i] == '+' || s[i] == '*') && (s[i+1] == '+' || s[i+1] == '*'))
            {
                wa = 1;
                break;
            }
        }

        if(wa)
        {
            printf("IMPOSSIBLE\n");
        }
        else
        {
            printf("%s\n", s);
        }

    }
    return 0;
}

标签:HDU,6342,0000,wa,int,Memories,00,000,&&
来源: https://blog.csdn.net/ygmn33/article/details/97031058