主题:[原创]子段和问题的扫描算法
Sedgewick
[专家分:0] 发布于 2008-01-06 14:30:00
// sum of the maximal continuous subsequence.
# include <iostream>
using namespace std;
int maximumSubsequenceSum(int x[], int n, int &seqStart, int &seqEnd)
{
int maxSum = 0, thisSum = 0;
for (int i = 0, j = 0; j < n; ++j) {
thisSum += x[j];
if (thisSum > maxSum) {
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
else if (thisSum < 0) {
i = j + 1;
thisSum = 0;
}
//cout << seqStart << ", " << seqEnd << endl;
}
return maxSum;
}
const int N = 50;
int main()
{
int array[N];
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> array[i];
}
int p, q;
int s = maximumSubsequenceSum(array, n, p, q);
cout << "x[" << p << ".." << q << "] = " << s << endl;
return 0;
}
[url=http://blog.csdn.net/Sedgewick]感兴趣的来我的窝看看。[/url]
最后更新于:2009-01-21 11:12:00
回复列表 (共4个回复)
沙发
Sedgewick [专家分:0] 发布于 2008-01-06 14:34:00
input:
10
31 -41 59 26 -53 58 97 -93 -23 84
output:
x[2..6] = 187
板凳
Sedgewick [专家分:0] 发布于 2008-01-06 14:45:00
31 -41 59 26 -53 58 97 -93 -23 84
0:↑↑
1:↑ ↑
2:↑ ↑
3: ↑↑
4: ↑ ↑
5: ↑ ↑
6: ↑ ↑
7: ↑ ↑
8: ↑ ↑
9: ↑ ↑
10: ↑ ↑
3 楼
justforfun626 [专家分:18460] 发布于 2008-01-07 00:36:00
To LZ:
Your program is not tested enough, in many cases, it gives wrong outputs.
Of course, sometimes, not always.
Suggestion:
1) Use a random number generator to generate the data array
2) Use an infinite loop in main to test.
Give you the array generator, which generate n-array in (-data_range, +data_range):
[code=c]
const int n=10;
const int data_range = 89;
int data[n];
void generateIntArray() {
srand(time(NULL));
int sign;
for(int i=0; i<n; i++) {
sign = (rand() % 2 == 0)? 1:-1;
data[i]=sign * rand() % data_range;
printf("%d ", data[i]);
}
printf("\n");
}
[/code]
Try to test and fix your algorithm
Thanks!
4 楼
Sedgewick [专家分:0] 发布于 2008-01-14 14:45:00
// pku 1050 to the max
#include <iostream>
using namespace std;
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
int maxsum0(int x[], int n)
{
int maxsofar = 0, maxendinghere = 0;
int i;
for (i = 0; i < n; ++i)
{
// invariant: maxendinghere and maxsofar are accurate for x[0..i-1]
maxendinghere = max(maxendinghere + x[i], 0);
maxsofar = max(maxsofar, maxendinghere);
}
return maxsofar;
}
int maximumSubsequenceSum(int x[], int n, int &seqStart, int &seqEnd)
{
int maxSum = 0, thisSum = 0;
for (int i = 0, j = 0; j < n; ++j) {
thisSum += x[j];
if (thisSum > maxSum) {
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}
else if (thisSum < 0) {
i = j + 1;
thisSum = 0;
}
//cout << seqStart << ", " << seqEnd << endl;
}
return maxSum;
}
const int N = 101;
int array[N][N];
int maxsum2D(int n, int m, int &s, int &t, int &u, int &v)
{
int maxsofar = 0, maxendinghere = 0;
int *tmpArr = new int[m + 1];
for (int i = 0; i < n; ++i) {
// void *memset(void *s, int c, size_t n);
memset(tmpArr, 0, sizeof(int)*n);
for (int j = i; j < m; ++j) {
for (int k = 0; k < m; ++k) { tmpArr[k] += array[j][k]; }
//printArray(tmpArr, m);
int col1, col2;
maxendinghere = maximumSubsequenceSum(tmpArr, m, col1, col2);
if (maxendinghere > maxsofar) {
maxsofar = maxendinghere;
s = i;
t = j;
u = col1;
v = col2;
}
}
}
delete tmpArr;
return maxsofar;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> array[i][j];
}
}
int s, t, u, v;
cout << maxsum2D(n, n, s, t, u, v)<<endl;
cout << " " << u << ".." << v << endl;
cout << s << endl << t << endl;
return 0;
}
我来回复