tugas 2 paal e agus budi raharjo 5109100164

25
Perancangan dan Analisis Algoritme Lanjut Agus Budi Raharjo 5109100164 Jurusan Teknik Informatika Fakultas Teknologi Informasi Institut Teknologi Sepuluh Nopember

Upload: budi-raharjo

Post on 26-Jun-2015

113 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Tugas 2 paal e agus budi raharjo 5109100164

Perancangan dan Analisis Algoritme Lanjut

Agus Budi Raharjo5109100164

Jurusan Teknik InformatikaFakultas Teknologi Informasi

Institut Teknologi Sepuluh Nopember

Page 2: Tugas 2 paal e agus budi raharjo 5109100164

Latihan

• UVA problem 10131 – Is Bigger Smarter• UVA problem 10069 – Distinct Subsequences• UVA problem 10154 – Weights and Measures• UVA problem 116 – Unidirectional TSP• UVA problem 10003 – Cutting Sticks• UVA problem 10261 – Ferry Loading• UVA problem 10271 – Chopsticks• UVA problem 10201 – Adventures in moving –

Part IV

Page 3: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

Problem EDistinct SubsequencesInput: standard input

Output: standard output A subsequence of a given sequence is just the given sequence with some elements (possibly none) left out. Formally, given a sequence X = x1x2…xm, another sequence Z = z1z2…zk is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1, 2, …, k, we have xij = zj. For example, Z = bcdb is a subsequence of X = abcbdab with corresponding index sequence < 2, 3, 5, 7 > .In this problem your job is to write a program that counts the number of occurrences of Z in X as a subsequence such that each has a distinct index sequence. InputThe first line of the input contains an integer N indicating the number of test cases to follow. The first line of each test case contains a string X, composed entirely of lowercase alphabetic characters and having length no greater than 10,000. The second line contains another string Z having length no greater than 100 and also composed of only lowercase alphabetic characters. Be assured that neither Z nor any prefix or suffix of Z will have more than 10100 distinct occurrences in X as a subsequence. OutputFor each test case in the input output the number of distinct occurrences of Z in X as a subsequence. Output for each input set must be on a separate line.

Page 4: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

Sample Input2babgbagbagrabbbitrabbit Sample Output53 __________________________________________________________________________________Rezaul Alam Chowdhury

Page 5: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

Langkah Penyelesaian :

- Fungsi biaya yang diperlukan adalah :

Banyak kemungkinan = Hn + Hn-1

(jika huruf pada kata kedua = huruf kata pertama)jika huruf pertama muncul, maka H1 ditambah 1

Page 6: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

Langkah Penyelesaian :1.Menggunakan dua dimensional, namun untuk implementasi cukup satu dimensi array2.Isi semua field dengan 03.Bandingkan huruf terakhir dengan string pertama, contoh : G == B4.Jika tidak sama, nilai tidak diubah5.Jika sama, maka biaya berlaku dengan syarat bukan huruf pertama6.Untuk huruf pertama yang sama (contoh B == B), maka nilai field ditambah 1

B A G

B 0 0 0

A 0 0 0

B 0 0 0

G 0 0 0

B 0 0 0

A 0 0 0

G 0 0 0

Page 7: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

1.Bandingkan huruf terakhir dengan string pertama, contoh : G == B2.Jika tidak sama, nilai tidak diubah3.Hal yang sama dilakukan pada A==B & B==B

B A G

B 0 0 0

A 0 0 0

B 0 0 0

G 0 0 0

B 0 0 0

A 0 0 0

G 0 0 0

4. Untuk B, karena awal huruf, maka nilainya ditambah 1

B A G

B 1 0 0

A 0 0 0

B 0 0 0

G 0 0 0

B 0 0 0

A 0 0 0

G 0 0 0

Page 8: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

5. Untuk baris selanjutnya, field A == A diisi dari field di atasnya ditambah field huruf didepannya , 1+ 0= 1, dan seterusnya.

B A G

B 1 0 0

A 0 1 0

B 0 0 0

G 0 0 0

B 0 0 0

A 0 0 0

G 0 0 0

6. Hasil akhir ditentukan oleh jumlah field pada huruf terakhir pada baris terakhir

B A G

B 1 0 0

A 1 1 0

B 2 1 0

G 2 1 1

B 3 1 1

A 3 4 1

G 3 4 5

Page 9: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10069 – Distinct Subsequences

Tips : karena kemungkinan besar ( > 20 digit), maka digunakan Biginteger pada Java

import java.util.*;import java.math.BigInteger;

public class Main {

public static void main(String[] args) {

int loop; String kata1 = new String(); String kata2 = new String(); Scanner ok = new Scanner(System.in); loop= Integer.parseInt(ok.nextLine()); for(int a=0;a<loop;a++) { kata1=ok.nextLine(); kata2=ok.nextLine(); BigInteger [] isi = new BigInteger[kata2.length()];

for(int b=0;b<kata2.length();b++) { isi[b]= BigInteger.ZERO; }

for(int b=0;b<kata1.length();b++) { for(int c=kata2.length()-1;c>=0;c--) { if(kata1.charAt(b)==kata2.charAt(c)) { if(c==0) { isi[c]=isi[c].add(BigInteger.ONE);} else { isi[c]= isi[c].add(isi[c-1]); } } } } System.out.println(isi[kata2.length()-1]); } }

}

Implementasi

Page 10: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSP Unidirectional TSP 

BackgroundProblems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Salesperson Problem (TSP) -- finding whether all the cities in a salesperson's route can be visited exactly once with a specified limit on travel time -- is one of the canonical examples of an NP-complete problem; solutions appear to require an inordinate amount of time to generate, but are simple to check. This problem deals with finding a minimal path through a grid of points while traveling only from left to right.

The ProblemGiven an matrix of integers, you are to write a program that computes a path of minimal weight. A path starts anywhere in column 1 (the first column) and consists of a sequence of steps terminating in column n (the last column). A step consists of traveling from column i to column i+1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 and m) of a matrix are considered adjacent, i.e., the matrix ``wraps'' so that it represents a horizontal cylinder. Legal steps are illustrated below. The weight of a path is the sum of the integers in each of the n cells of the matrix that are visited. For example, two slightly different matrices are shown below (the only difference is the numbers in the bottom row). The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows.

Page 11: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSP

The InputThe input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by integers where m is the row dimension and n is the column dimension. The integers appear in the input in row major order, i.e., the first n integers constitute the first row of the matrix, the second n integers constitute the second row and so on. The integers on a line will be separated from other integers by one or more spaces. Note: integers are not restricted to being positive. There will be one or more matrix specifications in an input file. Input is terminated by end-of-file. For each specification the number of rows will be between 1 and 10 inclusive; the number of columns will be between 1 and 100 inclusive. No path's weight will exceed integer values representable using 30 bits. The OutputTwo lines should be output for each matrix specification in the input file, the first line represents a minimal-weight path, and the second line is the cost of a minimal path. The path consists of a sequence of n integers (separated by one or more spaces) representing the rows that constitute the minimal path. If there is more than one path of minimal weight the path that is lexicographically smallest should be output.

Page 12: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSPSample Input5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 8 6 4 5 6 3 4 1 2 8 6 6 1 8 2 7 4 5 9 3 9 9 5 8 4 1 3 2 6 3 7 2 1 2 3 2 2 9 10 9 10

Sample Output1 2 3 4 4 5 16 1 2 1 5 4 5 11 1 1 19

Page 13: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSP

Langkah Penyelesaian :1.Mulai membaca dari array pojok kanan atas dengan index [baris][kolom-1]. 2.Satu field memiliki tiga kemungkinan setelahnya, yakni pada koordinat [baris-1][kolom+1], [baris][kolom+1], [baris+1][kolom+1]. Bandingkan dan cari jumlah paling kecil dari field, menunjukkan jarak yang ditempuh3.Buat satu array berisi pointer index berikutnya.4.Jika nilainya sama, pilih index baris terkecil

1

1 8

3

3 4 1 2 8 6

6 1 8 2 7 4

5 9 3 9 9 5

8 4 1 3 2 6

3 7 2 8 6 4

Page 14: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSP#include<stdio.h>

int main(){ int row, col; while(scanf("%d %d",&row,&col)>1) { int papan[row][col],asli, pointer[row][col], min; for(int a=0;a<row;a++) for(int b=0;b<col;b++) scanf("%d", &papan[a][b]); for(int b=col-2;b>=0;b--) { for(int a=0;a<row;a++) { if(a==0) { asli = papan[a][b]; papan[a][b]=asli+papan[row-1][b+1]; pointer[a][b]=1; if(papan[a][b] > asli+papan[a][b+1]) { papan[a][b]=asli+papan[a][b+1]; pointer[a][b]=2; } if(papan[a][b] > asli+papan[a+1][b+1]) { papan[a][b]=asli+papan[a+1][b+1]; pointer[a][b]=3; } }

Implementasi

Page 15: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSPelse if(a==row-1) { asli = papan[a][b]; papan[a][b]=asli+papan[a-1][b+1]; pointer[a][b]=1; if(papan[a][b] >asli+papan[a][b+1]) { papan[a][b]=asli+papan[a][b+1]; pointer[a][b]=2; } if(papan[a][b] > asli+papan[0][b+1]) { papan[a][b]=asli+papan[0][b+1]; pointer[a][b]=3; } } else { asli = papan[a][b]; papan[a][b]=asli+papan[a-1][b+1]; pointer[a][b]=1; if(papan[a][b] >asli+papan[a][b+1]) { papan[a][b]=asli+papan[a][b+1]; pointer[a][b]=2; } if(papan[a][b] >asli+papan[a+1][b+1]) { papan[a][b]=asli+papan[a+1][b+1]; pointer[a][b]=3; } } } }

Implementasi

Page 16: Tugas 2 paal e agus budi raharjo 5109100164

UVA 116 - Unidirectional TSPmin = papan[0][0]; for(int a=0;a<row;a++) if(papan[a][0]<min) min = papan[a][0]; for(int a=0;a<row;a++) { if(papan[a][0]==min) { for(int b=0;b<col;b++) { printf("%d ", a+1); if(pointer[a][b]==1) { if(a==0) a=row-1; else a=a-1; } else if(pointer[a][b]==2) a=a; else if(pointer[a][b]==3) { if(a==0) a=row-1; else a=a+1; }

} } break; } printf("\n%d\n", min); } return 0;}

Implementasi

Page 17: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

  Cutting Sticks

You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they only make one cut at a time. It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 = 20, which is a better price. Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.

Input  The input will consist of several input cases. The first line of each test case will contain a positive number l that represents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n < 50) of cuts to be made. The next line consists of n positive numbers ci ( 0 < ci < l) representing the places where the cuts have to be done, given in strictly increasing order. An input case with l = 0 will represent the end of the input.

Output  You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.

Page 18: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

Sample Input  100 3 25 50 75 10 4 4 5 7 8 0

Sample Output  The minimum cutting is 200. The minimum cutting is 22.

Miguel Revilla 2000-08-21

Page 19: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

Page 20: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

Page 21: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

Page 22: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

Page 23: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks#include <stdio.h>#define MAXN 55

int S[MAXN][MAXN];int A[MAXN];int N, L;

void Ini(){ int i; for(i=0;i<=N;i++) { S[i][i] = 0; S[i][i+1] = 0; }}

void Cal(){ int i,j,k,l; int inf = 1000000; int n = N+1,q; for(l=2;l<=n;l++) { for(i=0;i<=n-1;i++) { j=i+l; S[i][j]= inf; for(k=i+1;k<j;k++) { q= S[i][k] + S[k][j] + A[j] -A[i]; if(S[i][j] >q) S[i][j] = q; } } } printf("The minimum cutting is %d.\n", S[0][n]);}

Implementasi

Page 24: Tugas 2 paal e agus budi raharjo 5109100164

UVA 10003 – Cutting Sticks

int main(){ int f=0,i; while(scanf("%d", &L)==1) { if(!L) return 0; scanf("%d", &N); if(f++) Ini(); for(i=1;i<=N;i++) scanf("%d", &A[i]); A[i]=L; Cal(); } return 0;}

Implementasi

Page 25: Tugas 2 paal e agus budi raharjo 5109100164

TOTAL SUBMISSIONS