2 条题解

  • 3
    @ 2024-7-30 22:23:26
    #include<bits/stdc++.h>
    using namespace std;
    int dfs(const string &a, int pos, vector<int> &ovo, int mxcoin = 0,    int step1 = 0, int step2 = 0) {
        if (pos >= a.size())
            return 0;
        if (ovo[pos] != -1)
            return ovo[pos];
        if (a[pos] == '@')
            mxcoin = 1;
        if (pos + 1 < a.size() && a[pos + 1] != '*') 
            step1 = dfs(a, pos + 1, ovo);
        if (pos + 2 < a.size() && a[pos + 2] != '*') 
            step2 = dfs(a, pos + 2, ovo);
        ovo[pos] = mxcoin + max(step1, step2);
        return ovo[pos];
    }
    int main() {
        int t = 0;
        cin >> t;
        while (t--) {
            int n = 0;
            cin >> n;
            string a;
            cin >> a;
            vector<int> ovo(n, -1);
            cout << dfs(a, 0, ovo) << endl;
        }
        return 0;
    }
    

    非常好 dfs ,爱来自基沃托斯 感觉这样写比楼上优雅,故发题解 还有,熟悉的,不使用 vector 只能度过一个比较失败的人生(bushi

  • 1
    @ 2024-3-10 15:34:53

    答案 `

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int money[50100];
    int main(){
        int t;
        cin >>t;
        for (int i = 0;i<t;i++){
            int n;
            string l;
            cin >>n>>l;
            int wg = 0;
            int rmb = 0;
            for (int j = 0;;j++){
                if (wg >= n - 1){
                    money[i] = rmb;
                    break;
                }
                if(l[wg + 1] == '.'){
                    if (wg + 1 >= n){
                        money[i] = rmb;
                        break;
                    }
                    wg++;
                }
                if(l[wg + 1] == '@'){
                    if (wg + 1 >= n){
                        money[i] = rmb;
                        break;
                    }
                    wg++;
                    rmb++;
                }
                else if(l[wg + 1] == '*' && wg + 2 >= l.size()){
                    money[i] = rmb;
                    break;
                }
                else if(l[wg + 1] == '*' && l[wg + 2] == '.'){
                    if (wg + 2 >= n){
                        money[i] = rmb;
                        break;
                    }
                    wg += 2;
                }
                else if(l[wg + 1] == '*' && l[wg + 2] == '@'){
                    if (wg + 2 >= n){
                        money[i] = rmb;
                        break;
                    }
                    wg += 2;
                    rmb++;
                }
                else if(l[wg + 1] == '*' && l[wg + 2] == '*'){
                    money[i] = rmb;
                    break;
                }
            }
        }
        for (int i = 0;i<t;i++){
            cout <<money[i]<<endl;
        }
    }
    
    • 1

    信息

    ID
    715
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    (无)
    递交数
    65
    已通过
    21
    上传者