C******a 发帖数: 32 | 1 考虑有相同元素情形.需要给出compact, clean code, C or C++.
Write a C++ program that print the first longest
ascending or descending contiguous substring for a vector of integers. Given
a vector
4, 2, 1, 2, 3, 4, 3, 5, 1, 2, 4, 6, 5
the program would print:
1, 2, 3, 4
算法不难,但写不出简短的clean code. 欢迎讨论. | b********h 发帖数: 119 | 2 void las(vector v)
{
vector dp(v.size(), 1);
pair max(0, 1);
for(int i = 1; i < v.size(); ++i)
{
if(v[i] > v[i-1]) {
dp[i] = dp[i-1]+1;
if(dp[i] > max.second)
{
max.second = dp[i];
max.first = i;
}
}
else
dp[i] = 1;
}
for(int i = max.first-max.second+1; i <= max.first; ++i)
cout<
cout<
} | C******a 发帖数: 32 | 3 Seems this only finds out ascending substrings, right?
【在 b********h 的大作中提到】 : void las(vector v) : { : vector dp(v.size(), 1); : pair max(0, 1); : for(int i = 1; i < v.size(); ++i) : { : if(v[i] > v[i-1]) { : dp[i] = dp[i-1]+1; : if(dp[i] > max.second) : {
|
|