标签:des style color io os ar for 数据 art
题目大意是:给你一串字符串,然后让你去截看看最少能截成几分。如果只包含大小写字母和空格就可以最多m个截成一个,有其他的字符那就最多n个截成一个。
数据很多直接dp[i][j]空间与时间都不能承受。dp[i][0]表示只有字母和空格,dp[i][1]表示有其他的字符。vis[i][0],vis[i][1]动态的记录长度。时间就会降为O(n)空间降为2*n。
input |
---|
10 15 On the 11-th of February, 2006 the contest "Timus Top Coders: First Challenge" is held! |
output |
8 |
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-12 ///#define M 1000100 #define LL __int64 ///#define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) using namespace std; const int maxn = 100010; int dp[maxn][2]; int vis[maxn][2]; int judge(char s) { if(s == ' ') return 1; if(s <= 'Z' && s >= 'A') return 1; if(s <= 'z' && s >= 'a') return 1; return 0; } char str[maxn]; int main() { int n, m; while(cin >>n>>m) { memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); getchar(); gets(str); int len = strlen(str); dp[0][1] = 1; vis[0][1] = 1; if(judge(str[0])) { dp[0][0] = 1; vis[0][0] = 1; } else { dp[0][0] = -1; vis[0][0] = 0; } for(int i = 1; i < len; i++) { if(judge(str[i])) { if(vis[i-1][0] >= m) { dp[i][0] = min(dp[i-1][1], dp[i-1][0])+1; vis[i][0] = 1; } else { if(vis[i-1][0] && dp[i-1][0] < dp[i-1][1]+1) { dp[i][0] = dp[i-1][0]; vis[i][0] = vis[i-1][0]+1; } else { dp[i][0] = dp[i-1][1]+1; vis[i][0] = 1; } } } else vis[i][0] = 0; if(vis[i-1][1] >= n) { if(vis[i-1][0]) dp[i][1] = min(dp[i-1][1], dp[i-1][0])+1; else dp[i][1] = dp[i-1][1]+1; vis[i][1] = 1; continue; } if(!vis[i-1][0] || dp[i-1][1] < dp[i-1][0]+1) { dp[i][1] = dp[i-1][1]; vis[i][1] = vis[i-1][1]+1; continue; } dp[i][1] = dp[i-1][0]+1; vis[i][1] = 1; } if(vis[len-1][0]) cout<<min(dp[len-1][0], dp[len-1][1])<<endl; else cout<<dp[len-1][1]<<endl; } return 0; }
标签:des style color io os ar for 数据 art
原文地址:http://blog.csdn.net/xu12110501127/article/details/39228765