Upcoming SlideShare
Loading in …5
×

# Tugas 2 paal e agus budi raharjo 5109100164

346 views
227 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

No Downloads
Views
Total views
346
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Tugas 2 paal e agus budi raharjo 5109100164

1. 1. Perancangan dan Analisis Algoritme Lanjut Agus Budi Raharjo 5109100164 Jurusan Teknik Informatika Fakultas Teknologi Informasi Institut Teknologi Sepuluh Nopember
2. 2. 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
3. 3. UVA 10069 – Distinct Subsequences Problem E Distinct Subsequences Input: 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. Input The 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. Output For 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.
4. 4. UVA 10069 – Distinct Subsequences Sample Input 2 babgbag bag rabbbit rabbit Sample Output 5 3 __________________________________________________________________________________ Rezaul Alam Chowdhury
5. 5. 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
6. 6. UVA 10069 – Distinct Subsequences Langkah Penyelesaian : 1. Menggunakan dua dimensional, namun untuk implementasi cukup satu dimensi array 2. Isi semua field dengan 0 3. Bandingkan huruf terakhir dengan string pertama, contoh : G == B 4. Jika tidak sama, nilai tidak diubah 5. Jika sama, maka biaya berlaku dengan syarat bukan huruf pertama 6. 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
7. 7. UVA 10069 – Distinct Subsequences B 1. Bandingkan huruf terakhir dengan string pertama, contoh : G == B 2. Jika tidak sama, nilai tidak diubah 3. Hal yang sama dilakukan pada A==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 B A G A 0 0 0 B 1 0 0 G 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
8. 8. UVA 10069 – Distinct Subsequences B A G B 1 0 0 A 0 1 0 B 0 0 0 G 0 0 0 B 5. Untuk baris selanjutnya, field A == A diisi dari field di atasnya ditambah field huruf didepannya , 1+ 0= 1, dan seterusnya. 0 0 0 B A G A 0 0 0 B 1 0 0 G 0 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 6. Hasil akhir ditentukan oleh jumlah field pada huruf terakhir pada baris terakhir
9. 9. UVA 10069 – Distinct Subsequences import java.util.*; import java.math.BigInteger; Implementasi 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]); } } } Tips : karena kemungkinan besar ( > 20 digit), maka digunakan Biginteger pada Java }
10. 10. UVA 116 - Unidirectional TSP Unidirectional TSP Background Problems 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 Problem Given 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.
11. 11. UVA 116 - Unidirectional TSP The Input The 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 Output Two 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.
12. 12. UVA 116 - Unidirectional TSP Sample Input 56 341286 618274 593995 841326 372864 56 341286 618274 593995 841326 372123 22 9 10 9 10 Sample Output 123445 16 121545 11 11 19
13. 13. 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 ditempuh 3. 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
14. 14. 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
15. 15. UVA 116 - Unidirectional TSP else 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
16. 16. UVA 116 - Unidirectional TSP min = 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) else if(pointer[a][b]==3) { if(a==0) a=row-1; else a=a+1; } } } break; } printf("n%dn", min); } return 0; } a=a; Implementasi
17. 17. 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.
18. 18. UVA 10003 – Cutting Sticks Sample Input 100 3 25 50 75 10 4 45780 Sample Output The minimum cutting is 200. The minimum cutting is 22. Miguel Revilla 2000-08-21
19. 19. UVA 10003 – Cutting Sticks
20. 20. UVA 10003 – Cutting Sticks
21. 21. UVA 10003 – Cutting Sticks
22. 22. UVA 10003 – Cutting Sticks
23. 23. 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
24. 24. UVA 10003 – Cutting Sticks Implementasi 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; }
25. 25. TOTAL SUBMISSIONS