2023CSP-S复赛消消乐题解

· · 题解

2023CSP-S复赛消消乐题解

题意解析:

这道题给出一个长度为 n 的字符串,求其中可以被消除的字串的个数。\ 下面是对于一个字符串能否被消除的形式化解释:

虽然但是好像貌似没什么用

思路1:

用 O(n^2) 的时间复杂度枚举每个字串,再用 O(n) 的时间复杂度用栈模拟字符串消除过程,若能消除,则将答案加1。\ \ 模拟的过程如下:

由于时间复杂度达到 O(n^3) ,仅能通过前七个测试点,所以预期得分30分。\ 代码如下:

#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
int n,ans;
string s;
bool judge(string a){
    stack<char>s1;
    stack<char>s2;
    for(int i=a.size()-1;i>=0;i--){
        s1.push(a[i]);
    }
    s2.push(s1.top());
    s1.pop();
    while(!s1.empty()){
        while(!s1.empty()&&!s2.empty()&&s2.top()==s1.top()){
            s1.pop();
            s2.pop();
        }
        if(!s1.empty())s2.push(s1.top()),s1.pop();
    }
    if(s2.empty()&&s1.empty()){
        return true;
    }
    return false;
}
int main(){
    cin>>n;
    cin>>s;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j+=2){
            if(judge(s.substr(i,j-i+1)))ans++;
        }
    }
    cout<<ans;
    return 0;
}

由此延申的第二种做法: DP 。

虽然复杂度相同,但是思路还是有部分差别的。\ 对于 dp[i][j] 有以下几种情况:

这样,第二种做法就出来了,通过枚举字符串的长度来解决。\ 代码如下:\ (虽然换了个思路,但是时间复杂度却没有优化,因此还是30分)

#include<iostream>
using namespace std;
int n,ans;
bool dp[10000][10000];
string str;
int main(){
    cin>>n;
    cin>>str;
    fill(&dp[0][0],&dp[0][0]+n*n,false); 
    for(int i=0;i<n-1;i++)
        dp[i][i+1]=(str[i]==str[i+1]);
    for(int len=3;len<=n;len++){
        for(int i=0;i<n;i++){
            int j=len+i-1;
            if(str[i]==str[j]&&dp[i+1][j-1]){
                dp[i][j]=true;
                continue;
            }
            for(int k=i;k<=j;k++){
                if(dp[i][k]&&dp[k+1][j]){
                    dp[i][j]=true;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if(dp[i][j])ans++;
    cout<<ans;
    return 0;
}

思路3:

既然可以用一个栈来模拟一个字符串被消除的过程,那么,为什么多个栈来模拟多个字符串呢?\ 由此,第三种做法——多个栈的模拟。\ 通过枚举字符串的起点,在模拟的过程中,若出现栈为空,即从起点到当前字符这一段的字符串可以被消除时,这就是一个答案,由于每次枚举的起点不同,所以,不存在同一个字串被重复计算的的情况。\ 这个思路用到了类似前缀和的方法。 代码如下:\ (优化后时间复杂度为 O(n^2) 可以应对 n \leq 8000 的数据,也就是前10个测试点,最终得分50分)

#include<iostream>
#include<stack>
using namespace std;
int n,ans;
string str;
int main(){
    cin>>n;
    cin>>str;
    for(int i=0;i<n;i++){
        stack<char>s;
        for(int j=i;j<n;j++){
            if(!s.empty()&&s.top()==str[j]){
                s.pop();
                if(s.empty()){
                    ans++;
                }
            }else s.push(str[j]);
        }
    }
    cout<<ans;
    return 0;
}

思路4:

本来这题到这里就结束了,因为一等奖分数线是150以上,二等奖更不用提。\ 好的接下来回归正题:\ 思路是用 map 记录每次压入栈后的状态,若一个状态多次出现,则之间的字符串可以被消除。\ 这里可以用一个 unordered\_map 来实现。压入过程中判断是否出现相同的字符串,出现则将答案加1。\ Q:为什么用 unordered\_map 不用 map ?\ A:首先,它的遍历速度普遍较快。(平均复杂度为 O(1) )。其次,本题不需要对键值排序。\ 接下来就是代码了:\ (这次的代码终于给力点,拿了60分)

#include<iostream>
#include<unordered_map>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int n,res;
string s;
stack<char>st;
unordered_map<string,int>state_count;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    cin>>s;
    state_count[""]=1;
    for(char ch:s){
        if(!st.empty()&&st.top()==ch){
            st.pop();
        }else{
            st.push(ch);
        }
        string state;
        stack<char>temp=st;
        while(!temp.empty()){
            state.push_back(temp.top());
            temp.pop();
        }
        reverse(state.begin(),state.end());
        res+=state_count[state];
        state_count[state]++;
    }
    cout<<res<<endl;
    return 0;
}

都有60分了,结束吧~~~\ (至少我想不出别的方法了)