HDU 1087

Bear and Strings

Problem Description

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

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

int a[1050];
int s,n,i,j;
int num[1050],now;
while(scanf("%d",&n)!=EOF)
{
now=0;
if(n==0)
break;
memset(a,0,sizeof(a));
memset(num,0,sizeof(num));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
num[i]=a[i];
}
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
if(a[i]>a[j]&&num[j]+a[i]>num[i])
{
num[i]=num[j]+a[i];
}
}
}
for(i=1;i<=n;i++)
{
if(num[i]>now)
now=num[i];
}
printf("%d\n",now);
}
return 0;
}

Codeforces 385B

Bear and Strings

Problem Description

The bear has a string s = s1s2… s|s| (record |s| is the string’s length), consisting of lowercase English letters. The bear wants to count the number of such pairs of indices i, j (1 ≤ i ≤ j ≤ |s|), that string x(i, j) = sisi + 1… sj contains at least one string “bear” as a substring.
String x(i, j) contains string “bear”, if there is such index k (i ≤ k ≤ j - 3), that sk = b, sk + 1 = e, sk + 2 = a, sk + 3 = r.
Help the bear cope with the given problem.

Input

The first line contains a non-empty string s (1 ≤ |s| ≤ 5000). It is guaranteed that the string only consists of lowercase English letters.

Output

Print a single number — the answer to the problem.

Sample Input

bearbtear

Sample Output

6

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

int a[5010];
char s[5010];
int i,now,sum,len,j,flag;
while(scanf("%s",s)!=EOF)
{
len=strlen(s);
now=1;
sum=0;
memset(a,0,sizeof(a));
for(i=3;i<len;i++)
{
if(s[i-3]=='b'&&s[i-2]=='e'&&s[i-1]=='a'&&s[i]=='r')
{
a[i]=1;
}
}
for(i=0;i<len;i++)
{
flag=-1;
for(j=i;j<len-3;j++)
{
if(s[j]=='b'&&s[j+1]=='e'&&s[j+2]=='a'&&s[j+3]=='r')
{
flag=j;
break;
}
}

if(flag!=-1)
{
//printf("%c\n",s[i]);
sum+=(len-flag-3);
//printf("sum+=len-3 %d %d\n",flag,len-flag-3);
}
}
printf("%d\n",sum);
}
return 0;
}

Codeforces 382A

Ksenia and Pan Scales

Problem Description

Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.
The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

Input

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol “|” indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.
The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.
It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

Output

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string “Impossible”. Otherwise, print the description of the resulting scales, copy the format of the input.
If there are multiple answers, print any of them.

Sample Input

AC|T
L

Sample Output

AC|TL

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

char a[30],b[30];
int l,r,i,j;
int lena,lenb;
while(scanf("%s",a)!=EOF)
{
scanf("%s",b);
lena=strlen(a);
lenb=strlen(b);
for(i=0;i<lena;i++)
{
if(a[i]=='|')
{
l=i;
r=lena-i-1;
break;
}
}
//printf("%d-%d\n",l,r);
if((lenb-abs(l-r))%2!=0||lenb<abs(l-r))
{
printf("Impossible\n");
}
else if(l<=r)
{
for(i=0;i<(lenb-(r-l))/2+r-l;i++)
printf("%c",b[i]);
printf("%s",a);
for(;i<lenb;i++)
{
printf("%c",b[i]);
}
printf("\n");
}
else if(l>r)
{
for(i=0;i<(lenb-(l-r))/2;i++)
printf("%c",b[i]);
printf("%s",a);
for(;i<lenb;i++)
{
printf("%c",b[i]);
}
printf("\n");


// printf("%s",a);
//printf("%s\n",b);
}
}
return 0;
}

Codeforces 382A

Ksenia and Pan Scales

Problem Description

Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.
The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

Input

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol “|” indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.
The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.
It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

Output

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string “Impossible”. Otherwise, print the description of the resulting scales, copy the format of the input.
If there are multiple answers, print any of them.

Sample Input

AC|T
L

Sample Output

AC|TL

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{

char a[30],b[30];
int l,r,i,j;
int lena,lenb;
while(scanf("%s",a)!=EOF)
{
scanf("%s",b);
lena=strlen(a);
lenb=strlen(b);
for(i=0;i<lena;i++)
{
if(a[i]=='|')
{
l=i;
r=lena-i-1;
break;
}
}
//printf("%d-%d\n",l,r);
if((lenb-abs(l-r))%2!=0||lenb<abs(l-r))
{
printf("Impossible\n");
}
else if(l<=r)
{
for(i=0;i<(lenb-(r-l))/2+r-l;i++)
printf("%c",b[i]);
printf("%s",a);
for(;i<lenb;i++)
{
printf("%c",b[i]);
}
printf("\n");
}
else if(l>r)
{
for(i=0;i<(lenb-(l-r))/2;i++)
printf("%c",b[i]);
printf("%s",a);
for(;i<lenb;i++)
{
printf("%c",b[i]);
}
printf("\n");


// printf("%s",a);
//printf("%s\n",b);
}
}
return 0;
}

HDU 1046

Gridland

Problem Description

For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the “easy” problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the “hard” ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North–South or East–West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 × 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 × 3-Gridland, the shortest tour has length 6.

Input

The first line contains the number of scenarios.
For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.

Output

The output for each scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.

Sample Input

2
2 2
2 3

Sample Output

Scenario #1:
4.00

Scenario #2:
6.00

Source

Northwestern Europe 2001

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{

float a;
int n,m;
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
printf("Scenario #%d:\n",i);
scanf("%d%d",&n,&m);
a=(float)(m*n);
if(n%2==0||m%2==0)
{
printf("%.2f\n\n",a);
}
else
{
a--;
a+=sqrt(2);
printf("%.2f\n\n",a);
}
}
return 0;
}