当前位置:网站首页>B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
2022-06-23 05:30:00 【Elucidation】
The question :
- Given a string , Find all of this string Continuous substrings in , The maximum number of characters minus the minimum number of characters Value What's the biggest .
Ideas :
- dp, String type , Try and 26 Letter connection
- ∣ s ∣ ≤ 1 0 6 ∣s∣≤10^6 ∣s∣≤106 , Pay attention to memory limitations .
Two codes , Detail Notes .
C o d e : Code: Code:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
#define oper(a) operator<(const a& ee)const
#define endl '\n'
#define ul (u << 1)
#define ur (u << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10, M = 2e5 + 10, mod = 1e8;
int INF = 0x3f3f3f3f; ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, k;
int dp[2][26][26];
// Just look at the words of the last two ,dp[j][k] representative front i In all consecutive substrings formed by bits ,
//j Most characters appear ,k The maximum difference in the case of the least number of characters
// Just look at the first place ,dp[0] The collection is j、k Characters may not exist ,
//dp[1] Collected information ,j、k All characters must exist , In this way, the maximum difference is legal
char s[N];
void solve() {
cin >> s + 1;
n = strlen(s + 1);
int ans = 0;
mem(dp[1], -0x3f);// None of the initial characters exist , illegal
for (int i = 1; i <= n; i++) {
int u = s[i] - 'a';
// What is the current character , Corresponding to the status of the affected part
for (int j = 0; j < 26; j++) {
if (u == j)continue;// The same is meaningless
// There are now u ,u、j State maximum difference ++
dp[0][u][j]++;
dp[1][u][j]++;
ans = max(ans, dp[1][u][j]);// Maintain the optimal solution at any time
}
for (int j = 0; j < 26; j++) {
if (u == j)continue;
// There are now u ,j、u State maximum difference --
dp[1][j][u]--;
dp[1][j][u] = max(dp[1][j][u], dp[0][j][u] - 1);
// Once there was one u, Accumulated after j、u The state can be legal
// from dp[0] Assign to dp[1]
dp[0][j][u] = max(dp[0][j][u] - 1, 0);
// dp[0] There is no need to maintain negative numbers
}
}
cout << ans;
}
int main() {
cinios;
int T = 1;
for (int t = 1; t <= T; t++)
solve();
return 0;
}
/* */
C o d e : Code: Code:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
#define oper(a) operator<(const a& ee)const
#define endl "\n"
#define ul (u << 1)
#define ur (u << 1 | 1)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int INF = 0x3f3f3f3f; ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, k;
struct node
{
int w, f1, f2;
}dp[26][26];
char s[N];
void solve() {
cin >> s + 1;
n = strlen(s + 1);
int ans = 0;
for (int i = 1; i <= n; i++) {
int u = s[i] - 'a';
for (int j = 0; j < 26; j++) {
if (u == j)continue;
dp[u][j].w++;
if (dp[u][j].f1)ans = max(ans, dp[u][j].w);
//f1 Mark j Whether the character exists , There is no answer that cannot be counted
dp[j][u].w = max(dp[j][u].w - 1, -1);
// The shortest time is -1, There is only one u Character as the beginning , No matter how short it is
dp[j][u].f1 = 1;//u Characters exist
if (dp[j][u].w == -1)dp[j][u].f2 = 1;// If u The beginning of the character is marked with
else if (dp[j][u].f2) {
// Meet again after u Character time , Obviously the first one u You can give up , Also does not affect the u The existence of characters
dp[j][u].w++;// length ++
dp[j][u].f2 = 0;
}
}
}
cout << ans;
}
int main() {
cinios;
int T = 1;
for (int t = 1; t <= T; t++)
solve();
return 0;
}
/* */
边栏推荐
猜你喜欢

Event日志关键字:EventLogTags.logtags

Calculate Euclidean distance and cosine similarity

VMware network connection error unit network service not found

insert into... Where not exists insert to avoid repeated use

App hangs~

Drag and drop frame

AMS:startActivity桌面启动应用

Konva series tutorial 1:what is konva?

Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling

Win软件 - (Net-Framework)已处理证书链,但是在不受信任提供程序信任的根证书中终止
随机推荐
Introduction to JDBC (I) DML operation
MCS: continuous random variable chi square distribution
九九乘法表.bat
Go language - custom error
Mongodb sharding principle
Drag and drop拖放框架
markdown给图片加背景色
About dos/ddos attack and defense
Zygote进程
Win11不能录制音频怎么办?Win11无法录入声音的解决方法
Mysql入门学习(三)之视图
Drama asking Huamen restaurant Weng
渗透测试基础 | 附带测试点、测试场景
Laravel8 implementation of picture verification code
数据库连接异常:create connection error, url: jdbc:mysql://ip/数据库名, errorCode 0, state 08S01问题处理
MCS:离散随机变量——Uniform分布
MCS:连续随机变量——Student’s t分布
Calculate Euclidean distance and cosine similarity
MCS: discrete random variable - uniform distribution
应用挂了~