Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
题意:求一个连续的和最大的序列 输出最大的和跟起始位置 结束位置
思路:有一个max1 总和s 只有s>max1时才更新max1更新end
会出现 start改变的情况 具体看标注3
有没有考虑过这种情况 就是前面很大 突然出现一个负数 这样就会出现start>end的情况 代码中1 2标注就防止了这种情况
。。。。。格式很头疼啊
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=500009;
const int INF=0x3f3f3f3f;
ll a[maxn];
int main()
{
ll T,n,i,j,k;
scanf("%lld",&T);
for(k=1;k<=T;k++)
{
scanf("%lld",&n);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
ll s=0,max1=-1e9,start=1,endd=1,t=1;
for(i=1;i<=n;i++)
{
s+=a[i];
if(max1<=s)
{
max1=s;
endd=i;
start=t;//1
}
if(s<0)//3
{
s=0;
t=i+1;//2
}
}
printf("Case %d:\n",k);
printf("%lld %lld %lld\n",max1,start,endd);
if(T!=k)
puts("");
}
return 0;
}
The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.
A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property.
A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.
Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names.
Input
Each line of the input contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters.
Input is terminated by end of file.
Output
For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable.
Sample Input
apple peach
ananas banana
pear peach
Sample Output
appleach
bananas
pearch
-
这道题是这样输出的,首先读取a的字符,读取时判断是否是公共字符,如果是就把a前面的字符全部输出,
-
然后就来判断b,(和判断a是一样的),然后把公共字符输出,一直这样判断,直到最后一个公共字符
-
当最后一个公共字符判断完了后,把剩下的输出,先输a,再b.
#include
#include
#include
using namespace std;
typedef long long ll;
int dp[110][110];
char a[110],b[110];
char s[220];
int main()
{
ll n,m,i,j,k;
while(~scanf("%s%s",a+1,b+1))
{
memset(dp,0,sizeof(dp));
n=strlen(a+1);
m=strlen(b+1);
for(i=1; i<=n; i++)
for(j=1; j<=m; j++) //求出最长公共子序列
{
if(a[i]==b[j])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
i=n;
j=m;
k=0;
while(dp[i][j]) //将要输出的字符串先到存到s数组中
{
if(a[i]==b[j]) //当a,b,中字符相等
{
s[k++]=a[i];
i--;
j--;
}
else if(dp[i][j-1]=dp[i-1][j])
{
s[k++]=b[j];
j--;
}
}
while(i>=1) //a中不存在公共子序列的部分
{
s[k++]=a[i];
i--;
}
while(j>=1) //b中不存在公共子序列的部分
{
s[k++]=b[j];
j--;
}
for(int i=k-1; i>=0; i--)
printf("%c",s[i]);
puts("");
}
return 0;
}
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the maximum according to rules, and one line one case.
Sample Input
3 1 3 2
4 1 2 3 4
4 3 3 2 1
0
Sample Output
4
10
3
题意:找最长上升序列的和
#include
#include
#include
#include
using namespace std;
typedef long long ll;
ll dp[1100000];
ll a[1100000];
int main()
{
ll n,i,j;
while(~scanf("%lld",&n))
{
if(n==0)
break;
for(i=1; i<=n; i++)
scanf("%lld",&a[i]);
memset(dp,0,sizeof(dp));
a[0]=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
if(a[i]>a[j])
dp[i]=max(dp[i],dp[j]+a[i]);
dp[i]=max(dp[i],a[i]);
}
ll max1=0;
for(i=1; i<=n; i++)
{
//cout<=max1)
max1=dp[i];
}
printf("%lld\n",max1);
}
return 0;
}