1007(最大子串和)
2013-03-08 21:33
337 查看
#include <iostream> using namespace std; const int MAX = 10000; int a[MAX]; void findij(const int array[], size_t length, unsigned int& i, unsigned int& j, const int& max) { int cur_max; for(i = 0; i <length; i++) { cur_max = 0; for(j=i; j<length; j++) { cur_max += array[j]; if(max == cur_max) return; } } } int Kadane(const int array[], size_t length, unsigned int& left, unsigned int& right) { unsigned int i, cur_left, cur_right; int cur_max, max; cur_max = max = left = right = cur_left = cur_right = 0; for(i = 0; i < length; ++i) { cur_max += array[i]; if(cur_max > 0) { cur_right = i; if(max < cur_max) { max = cur_max; left = cur_left; right = cur_right; } } else { cur_max = 0; cur_left = cur_right = i + 1; } } return max; } int main() { unsigned int i, j; size_t count; bool flag = true; cin>>count; for(i=0; i<count; i++) { cin>>a[i]; if(a[i]>=0) flag = false; } if(flag) { cout<<0<<" "<<a[0]<<" "<<a[count-1]<<endl; } else { int max = Kadane(a, count, i, j); findij(a, count, i, j, max); cout<<max<<" "<<a[i]<<" "<<a[j]<<endl; } return 0; }
相关文章推荐
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
- PAT 1007 Maximum Subsequence Sum(最大子串和)
- 最长公共子串、最长公共子序列、最长回文子串、模式匹配、最大子序列--字符串问题整理
- Leetcode005--字符串中最大的回文子串
- 找到含有n个不同字符的子串的最大长度
- 使用数据结构,最大上升子串问题
- 求一个整形数组的和最大的连续子串 -------- 程序员面试金典
- 51nod 1272 最大距离 O(nlog(n)) , 快排 , 最大连续子串
- hdu1087 最大上升子串和
- java实现字符串匹配求两个字符串的最大公共子串
- HDU 1087 Super Jumping! Jumping! Jumping!(最大递增子串和)
- nyistoj44--最大连续子串和
- 求两个字符串中的最大相同子串 SubString
- 最大公共子串
- 最大连续子串和
- [算法]最大子串和
- 最大公共子串
- 最大子串和
- 最大公共子串
- 动态规划算法求两个字符串的最大公共子串