LightOJ 1051 Good or Bad

題意:給你一個字符串,只包含大寫字母和‘?’,如果字符串中出現了連續三個以上的元音字母或者連續五個以上的輔音字母,則這個字符串是bad,不然就是good.

‘?’號可以替換任意字母,即可bad有可good,則輸出Mixed.


分析:dpy[i][j]表示到i爲止,連續j個元音可否達到。dpf表示輔音。

            這個轉移當達到3個元音或者5個輔音時後面就不在轉移了,即全是零。  所以找是否是mixed只需掃最後一個位置的前兩個元音或者前4個輔音是否不爲0。

            不懂可以打印下屏蔽的代碼,研究下。



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<sstream>
#include<limits>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N= 2000;
const int maxn = 1e2+10;
const int maxm = 2000000001;
const int MOD = 1000;
int dpy[maxn][maxn],dpf[maxn][maxn];   //元音  輔音  到i爲止,連續j個元音可否達到
char str[maxn];
int main(){
#ifdef LOCAL
	freopen("C:\\Users\\lanjiaming\\Desktop\\acm\\in.txt","r",stdin);
	//freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
    int T,kase = 0;
    cin>>T;
    while(T--)
    {
        cout<<"Case "<<++kase<<": ";
        cin>>str;
        int len = strlen(str);
        memset(dpy,0,sizeof(dpy));
        memset(dpf,0,sizeof(dpf));
        for(int i = 0; i < len; i++)
        {
            int x;
            if (str[i] == '?') x = 0;
            else if (str[i] == 'A' || str[i]=='E' || str[i]=='I' || str[i]=='O'||str[i]=='U')
                  x = 1;
                 else x = 2;
            if (i==0)
            {
                if ( x == 1 || x == 0) dpy[0][1] = 1;
                if ( x == 2 || x == 0) dpf[0][1] = 1;
            }else
             {
                 for(int j = 0; j <= 2; j++)
                    if (dpy[i-1][j])
                    {
                         if ( x == 1 || x == 0) dpy[i][j+1] = 1;
                         if ( x == 2 || x == 0) dpf[i][1] = 1;
                    }
                 for(int j = 0; j <= 4; j++)
                    if (dpf[i-1][j])
                    {
                         if ( x == 1 || x == 0) dpy[i][1] = 1;
                         if ( x == 2 || x == 0) dpf[i][j+1] = 1;
                    }
             }
        }
       /* for(int i = 0; i < len; i++)
        {
            cout<<i<<endl;
            for(int j = 0; j <= 3; j++) cout<<dpy[i][j]<<" ";cout<<endl;
            for(int j = 0; j <= 5; j++) cout<<dpf[i][j]<<" ";cout<<endl;
        }*/

        bool ok1 =false,ok2 = false;
        for(int i = 0; i < len; i++)
           if (dpy[i][3] || dpf[i][5]) ok2 =true;
           
        for(int i = 0; i <= 2; i++)
             if (dpy[len-1][i]) ok1 =true;
        for(int i = 0; i <= 4; i++)
             if (dpf[len-1][i]) ok1 =true;
             
        if (ok1 && ok2 ) cout<<"MIXED\n";
        else if (ok1) cout<<"GOOD\n";
              else cout<<"BAD\n";
    }
	return 0;
}

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