每日刷题(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;
}