2020年第十一届蓝桥杯国赛C++B 组题——本质上升序列

每日刷题(123)

本质上升序列

题目描述

在这里插入图片描述

C++代码(LIS暴力法)

这个方法来自https://blog.csdn.net/xjx19991226/article/details/117407391?spm=1001.2014.3001.5501

#include<bits/stdc++.h> 
typedef long long ll;
using namespace std;

int main()
{
    map<string,int> mp;
    string st, ss;
    st="tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
    ll ans = 0;
    queue<pair<string,int> > q;
    for(int i = 0; i < st.length(); i++)
    {
        ss="";
        ss += st[i];
        if(mp[ss] != 1)
        {
            mp[ss] = 1;
            q.push({ss,i});
            ans++;
        }
    }
    while(!q.empty())
    {
        ss = q.front().first;
        int xb = q.front().second;
        q.pop();
        for(int i = xb + 1; i < st.length(); i++)
        {
            if(st[i] > ss[ss.length() - 1] && mp[ss + st[i]] != 1)
            {
                q.push({ss + st[i], i});
                mp[ss + st[i]] = 1;
                ans++;
            }
        }
    }
    cout << ans <<endl;
    return 0;
}

运行结果:
在这里插入图片描述
所以答案就是3616159

郑未法

反正粗略一看,这方法我没看懂。。。

有看懂的大佬可以在评论区聊聊你的看法

#include<bits/stdc++.h>
using namespace std;
const int N = 2e2 + 10;
int last[N] , pos[N][30];
char s[N]; 
signed main()
{
    cin >> s + 1;
    int n = strlen(s + 1);
    for(int i = 1 ; i <= n ; i ++)
    {
        // 决策2 
        int sum = 0; 
        for(int j = 0 ; j <= 25 ; j ++) pos[i][j] = pos[i - 1][j];
        // 决策 1         
        pos[i][s[i] - 'a'] ++; // Si 作为一个长度为 1 的上升子序列     
        for(int k = 0 ; k < s[i] - 'a' ; k ++) sum += pos[i - 1][k];
        pos[i][s[i] - 'a'] += sum;    
        pos[i][s[i] - 'a'] -= pos[last[s[i] - 'a']][s[i] - 'a'];
        last[s[i] - 'a'] = i;
    }
    int ans = 0;
    for(int j = 0 ; j <= 25 ; j ++) ans += pos[n][j];
    cout << ans << '\n'; 
    return 0;
}

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值