uva10891,

## Problem:

Problem E
Game of Sum
Input File: e.in
Output: Standard Output

This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

### Input

The input consists of a number of cases. Each case starts with a line specifying the integer n (0 < n ≤100), the number of elements in the array. After that, n numbers are given for the game. Input is terminated by a line where n=0.

Output
For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.

# Sample Input                                Output for Sample Input

4
4 -10 -20 7
4
1 2 3 4
0
7

#### 10

Problem setter: Syed Monowar Hossain

## Source Code:

import java.util.StringTokenizer;

/**
* cost[x][y][z] represents the maximum sum of array from x to y for user z(0
* for user A, 1 for user B).(y>=x)
*
* cost[x][y][z] = sum from x to y (inclusive) subtract the cost[x][index][z^1]
* or cost[index][y][z^1];
*/

/**
* @author antonio081014
* @time: Mar 14, 2012, 3:35:03 PM
*/
class Main {

public static final int SIZE = 110;

// Mark if (x,y,z) is visited or not;
public boolean[][][]    mark;
// The maximum sum of array from x to y for user z. (y>=x)
public int[][][]        cost;
public int[]            data;
public int              N;

public static void main(String[] args) throws Exception {
Main main = new Main();
main.run();
System.exit(0);
}

public void run() throws Exception {
cost = new int[SIZE + 1][SIZE + 1];
mark = new boolean[SIZE + 1][SIZE + 1];
data = new int[SIZE + 1];
while ((N = Integer.parseInt(br.readLine())) != 0) {
for (int i = 1; i <= N; i++) {
data[i] = data[i - 1] + Integer.parseInt(tz.nextToken());
}

init();
System.out.println(2 * solve(1, N, 0) - data[N]);
}
}

public void init() {
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
for (int z = 0; z < 2; z++) {
cost[i][j][z] = Integer.MIN_VALUE;
mark[i][j][z] = false;
}
}

public int solve(int x, int y, int z) {
if (mark[x][y][z])
return cost[x][y][z];
if (x > y || x == 0 || y == 0) {
mark[x][y][z] = true;
return cost[x][y][z] = 0;
}

// The most remarkable part of this solution;
for (int tmp = 1; tmp <= y - x + 1; tmp++) {
cost[x][y][z] = Math.max(cost[x][y][z], data[y] - data[x - 1]
- solve(x + tmp, y, z ^ 1));
cost[x][y][z] = Math.max(cost[x][y][z], data[y] - data[x - 1]
- solve(x, y - tmp, z ^ 1));
}
mark[x][y][z] = true;
return cost[x][y][z];
}
}

## Monday, March 12, 2012

uva10029, poj2564, zoj1876(not passed),

## Problem C: Edit Step Ladders

An edit step is a transformation from one word x to another word y such that x and y are words in the dictionary, and x can be transformed to y by adding, deleting, or changing one letter. So the transformation from dig to dog or from dog to do are both edit steps. An edit step ladder is a lexicographically ordered sequence of words w1, w2, ... wn such that the transformation from wi to wi+1 is an edit step for all ifrom 1 to n-1.
For a given dictionary, you are to compute the length of the longest edit step ladder.

### Input

The input to your program consists of the dictionary - a set of lower case words in lexicographic order - one per line. No word exceeds 16 letters and there are no more than 25000 words in the dictionary.

### Output

The output consists of a single integer, the number of words in the longest edit step ladder.

```cat
dig
dog
fig
fin
fine
fog
log
wine
```

`5`

## Source Code:

import java.util.ArrayList;
import java.util.List;

/**
* Problem 10029, Edit Step Ladders
* Solution: The main idea is using DP to calculate the longest step distance by
* the ith word. It's necessary to try all the possible operation, which
* includes change, add, and delete. Each operation is one step distance. Then,
* the best part is using binary search to check the updated word with one step
* distance based on the current word in the dictionary(list).
*/

/**
* @author antonio081014
* @since Mar 10, 2012, 4:19:33 PM
*/
class Main {

private List<String> list;
private int          N;
private int[]        cost;

public static void main(String[] args) throws Exception {
Main main = new Main();
main.run();
System.exit(0);
}

public void run() throws Exception {
list = new ArrayList<String>();
String strLine = null;
while ((strLine = br.readLine()) != null && strLine.length() > 0) {
}
N = list.size();
cost = new int[N + 1];
cost = 1;
String a;
int max = 0;
for (int i = 1; i < N; i++) {
cost[i] = 1;
strLine = list.get(i);
int len = strLine.length();
for (int flag = 0; flag < 3; flag++) {
for (int j = 0; j < len; j++) {
for (int k = 0; k < 26; k++) {
a = trans(strLine, (char) ('a' + k), j, flag);
if (strLine.compareTo(a) < 0) {
break;
}
int mid = binarySearch(a, i);
if (mid >= 0 && cost[i] < cost[mid] + 1)
cost[i] = cost[mid] + 1;
}
}
}
for (int k = 0; k < 26; k++) {
a = trans(strLine, (char) ('a' + k), len, 0);
if (strLine.compareTo(a) < 0) {
break;
}
int mid = binarySearch(a, i);
if (mid >= 0 && cost[i] < cost[mid] + 1)
cost[i] = cost[mid] + 1;
}
if (cost[i] > max)
max = cost[i];
}
System.out.println(max);
}

public String add(String a, char c, int idx) {
return a.substring(0, idx) + c + a.substring(idx);
}

public String del(String a, int idx) {
return a.substring(0, idx) + a.substring(idx + 1);
}

public String chg(String a, char c, int idx) {
return a.substring(0, idx) + c + a.substring(idx + 1);
}

public String trans(String a, char c, int idx, int flag) {
switch (flag) {
case 0:
case 1:
return del(a, idx);
default:
return chg(a, c, idx);
}
}

// The boundary is different.
public int binarySearch(String s, int end) {
int left = 0;
int right = end - 1;
while (left <= right) {
int mid = ((left + right) / 2);
int tmp = s.compareTo(list.get(mid));
if (tmp == 0)
return mid;
else if (tmp < 0)
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
}