uva111258(String Partition)

来源:互联网 发布:51单片机寄存器有哪些 编辑:程序博客网 时间:2024/05/18 16:55

题意:题目说的是给定一串数,然后堆砌进行分割,使得每个分割快的值不大于int的最大值,,,然后求所有分割块的最大和。int的最大值也就10位数,那么我们就可以枚举位数,然后递推。

d[i]表示以i结尾的最大和,然后往前递推j位数,那么[i,j]的值与dp[i-j]的和再来与dp[i]去最大。

// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;LL dp[510];int n;char s[510];void solve(){int len = strlen(s);memset(dp, 0,sizeof dp);for (int i=1;i<=len;i++){for (int j = 1;j <= 10 && j <= i;j++){LL sum = 0LL;for (int k = 0;k < j;k++){sum = sum * 10 + s[i - j + k] - '0';if (sum >= 0 && sum <= INT_MAX)dp[i] = max(dp[i - j] + sum,dp[i]);else break;}}}cout << dp[len] << endl;}int main(){// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout);int t;scanf("%d%*c",&t);while(t--){gets(s);// cout << s << endl;solve();}return 0;}


0 0
原创粉丝点击