#30638: 70分,不知道問題出在哪裡


e002933 (徐MAN)

學校 : 不指定學校
編號 : 158405
來源 : [111.71.113.217]
最後登入時間 :
2023-11-18 16:56:51
c462. apcs 交錯字串 (Alternating Strings) -- apcs | From: [118.150.201.99] | 發表日期 : 2022-06-02 18:16

我把k=1還有k!=1的部份分開討論

前五個資測應該是k=1的都過了,我想問題是出在下半段

我的答案總是比資測的答案大

以下是程式碼:

#include<iostream>
using namespace std;
#include<cctype>
#include<cstring>

char a[100000];

int main() {
    int k;
    cin>>k;
    cin>>a;
    int ans = 1, total = 0;

    if(k == 1) {
        if(strlen(a) == 1)
            total = 1;
        else{
            for(int i = 0; i < strlen(a)-1; i++) {
                bool isup = isupper(a[i]) ? 1 : 0;
                if(isupper(a[i+1]) && !isup)
                    ans++;
                else if(islower(a[i+1]) && isup)
                    ans++;
                else
                    ans = 1;
                if(ans > total)
                    total = ans;
            }
        }
    }
    else {
        ans = 0;
        bool isup;
        for(int i = 0; i < strlen(a)-1; i++) {
            if(!ans)
                isup = isupper(a[i]) ? 1 : 0;
            if(isup) {
                int num = 1;
                for(int j = i+1; j <= i+k-1; j++) {
                    if(isupper(a[j])) {
                        num++;
                    }
                    else {
                        ans = 0;
                        break;
                    }
                }
                if(num == k) {
                    ans+=k;
                    i += k-1;
                    isup = 0;
                }
            }
            else {
                int num = 1;
                for(int j = i+1; j <= i+k-1; j++) {
                    if(islower(a[j])) {
                        num++;
                    }
                    else {
                        ans = 0;
                        break;
                    }
                }
                if(num == k) {
                    ans+=k;
                    i += k-1;
                    isup = 1;
                }
            }
            if(ans > total)
                total = ans;
        }
    }
    cout<<total;
    return 0;
}

 

謝謝版友們的回答

 
#30653: Re: 70分,不知道問題出在哪裡


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c462. apcs 交錯字串 (Alternating Strings) -- apcs | From: [118.160.180.56] | 發表日期 : 2022-06-03 20:10

我把k=1還有k!=1的部份分開討論

前五個資測應該是k=1的都過了,我想問題是出在下半段

我的答案總是比資測的答案大

以下是程式碼:

#include
using namespace std;
#include
#include

char a[100000];

int main() {
    int k;
    cin>>k;
    cin>>a;
    int ans = 1, total = 0;

    if(k == 1) {
        if(strlen(a) == 1)
            total = 1;
        else{
            for(int i = 0; i < strlen(a)-1; i++) {
                bool isup = isupper(a[i]) ? 1 : 0;
                if(isupper(a[i+1]) && !isup)
                    ans++;
                else if(islower(a[i+1]) && isup)
                    ans++;
                else
                    ans = 1;
                if(ans > total)
                    total = ans;
            }
        }
    }
    else {
        ans = 0;
        bool isup;
        for(int i = 0; i < strlen(a)-1; i++) {
            if(!ans)
                isup = isupper(a[i]) ? 1 : 0;
            if(isup) {
                int num = 1;
                for(int j = i+1; j <= i+k-1; j++) {
                    if(isupper(a[j])) {
                        num++;
                    }
                    else {
                        ans = 0;
                        break;
                    }
                }
                if(num == k) {
                    ans+=k;
                    i += k-1;
                    isup = 0;
                }
            }
            else {
                int num = 1;
                for(int j = i+1; j <= i+k-1; j++) {
                    if(islower(a[j])) {
                        num++;
                    }
                    else {
                        ans = 0;
                        break;
                    }
                }
                if(num == k) {
                    ans+=k;
                    i += k-1;
                    isup = 1;
                }
            }
            if(ans > total)
                total = ans;
        }
    }
    cout<    return 0;
}

 

謝謝版友們的回答


有點看不懂你的程式碼,似乎是第k+1個字沒有檢查大小寫。例如測資是

2
AAAa
 
ZeroJudge Forum