天天看點

ZZUOJ-10434: good string

Description

給定一個字元串,判斷它是否是good string。

good string定義為:

① 字元s是good string,字元p是good string,字元y也是good string

② P和Q都是good string,則PQ是good string

③ P是good string,則(P)是good string

④ P是good string,則!P是good string

⑤ P和Q都是good string,則P|Q和P&Q是good string

Input

輸入包含多組資料。每組資料為一行一個字元串,長度不超過100。

Output

對于每組資料,如果P是good string則輸出”P is a good string”,否則輸出”P is not a good string”。

Sample Input

!spy

!(s|p!y)

)sp|y

Sample Output

!spy is a good string

!(s|p!y) is a good string

)sp|y is not a good string

鄭州大學第九屆ACM大學生程式設計競賽題目。

/*
 * 分類:區間DP
 * dp[i][j]表示i到j之間的那個字元串是否為good string
 * 我們所求答案即為dp[1][len]
 * dp轉移方程見下列代碼
 *
 *  Name: 10434: good string
 *  Copyright: __X
 *  Author: long_long_ago
 *  Date: 18/12/15 08:25
 *  Description: http://acm.zzu.edu.cn:8000/problem.php?id=10434
 *  
 */

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 105

using namespace std;
int dp[N][N];
char str[N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, j, k, l, len, s, e;
    bool flag;
    while(~scanf("%s", str+))
    {
        flag = false;
        len = strlen(str+);
        for (i = ; i <= len; i++)
        {
            if (str[i] == 's' || str[i] == 'p' || str[i] == 'y' || str[i] == '|' || str[i] == '&'
                || str[i] == '(' || str[i] == ')' || str[i] == '!')
            {
                continue;
            }
            else
            {
                cout << str+ << " is not a good string" << endl;
                flag = true;
                break;
            }
        }
        if (flag)   continue;
        memset(dp, , sizeof(dp));
        for (i = ; i <= len; i++)
        {
            if (str[i] == 's' || str[i] == 'p' || str[i] == 'y')    dp[i][i] = ;
        }
        for (l = ; l <= len; l++)
        {
            for (s = ; s <= len - l + ; s++)
            {
                e = s + l - ;
                if (e-s >=  && str[s] == '(' && str[e] == ')' && dp[s+][e-])
                {
                    dp[s][e] = ;
                    continue;
                }
                if (str[s] == '!' && dp[s+][e])
                {
                    dp[s][e] = ;
                    continue;
                }
                for(j = s + ; j < e; j++)
                {
                    if (dp[s][j-] && dp[j+][e])
                    {
                        if (str[j] == '|' || str[j] == '&')
                        {
                            dp[s][e] = ;
                            break;
                        }
                    }
                }
                for(k = s; k < e; k++)
                {
                    if (dp[s][k] &&dp[k+][e])
                    {
                        dp[s][e] = ;
                        break;
                    }
                } 
            }
        }
        if(dp[][len])
            printf("%s is a good string\n", str + );
        else printf("%s is not a good string\n", str + );
    }
    return ;
}

           

下面是某大神的寫法 (╯▽╰)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
typedef long long LL;
const int N =  + ;
char s[N];

char f[] = "spy!|&()";
int sc(char x)
{
    for(int i = ; i < ; i ++)
        if(x == f[i])
            return ;
    return ;
}

int check()
{
    stack<char> kh;
    while(!kh.empty())kh.pop();
    for(int i = ; s[i]; i ++)
    {
        if(!sc(s[i]))
            return ;
        if(s[i] == '(')
            kh.push('(');
        else if(s[i] == ')')
        {
            if(kh.empty())
                return ;
            else
                kh.pop();
            if(s[i-] == '|' || s[i-] == '&' || s[i-] == '(' || s[i-] == '!')
                return ;
        }
        else if(s[i] == '|' || s[i] == '&')
            if(i <  || s[i-] == '|' || s[i-] == '&' || s[i-] == '!' || s[i-] == '(')
                return ;
    }

    int len = strlen(s);
    if(s[len-] == '|' || s[len-] == '&' || s[len-] == '!')
        return ;

    return kh.empty();
}

int main()
{
    while(gets(s))
        printf("%s %s\n", s, check() ? "is a good string" : "is not a good string");
    return ;
}