SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
Perancangan dan Analisis
Algoritme Lanjut
Agus Budi Raharjo
5109100164
Jurusan Teknik Informatika
Fakultas Teknologi Informasi
Institut Teknologi Sepuluh Nopember
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
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.
UVA 10069 – Distinct Subsequences
Sample Input
2
babgbag
bag
rabbbit
rabbit
Sample Output
5
3
__________________________________________________________________________________
Rezaul Alam Chowdhury
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
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
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
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
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

}
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.
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.
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
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
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
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
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
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.
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
UVA 10003 – Cutting Sticks
UVA 10003 – Cutting Sticks
UVA 10003 – Cutting Sticks
UVA 10003 – Cutting Sticks
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
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;
}
TOTAL SUBMISSIONS

Contenu connexe

Tendances

Gender and Age Detection using OpenCV.pptx
Gender and Age Detection using OpenCV.pptxGender and Age Detection using OpenCV.pptx
Gender and Age Detection using OpenCV.pptxSakshiVishwakarma12
 
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHM
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHMHEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHM
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHMamiteshg
 
Multiple object detection report
Multiple object detection reportMultiple object detection report
Multiple object detection reportManish Raghav
 
Iot and cloud computing on pervasive healthcare
Iot and cloud computing on pervasive healthcareIot and cloud computing on pervasive healthcare
Iot and cloud computing on pervasive healthcareMd Nazrul Islam Roxy
 
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...huong Tran thu
 
Ubiquitous computing
Ubiquitous computingUbiquitous computing
Ubiquitous computingPriti Punia
 
Thermal Infrared Face Recognition
Thermal Infrared Face RecognitionThermal Infrared Face Recognition
Thermal Infrared Face RecognitionTarun Jaiswal
 
Reconnaissance d'ecriture manuscrite
Reconnaissance d'ecriture manuscriteReconnaissance d'ecriture manuscrite
Reconnaissance d'ecriture manuscriteAfaf MATOUG
 
Predicting Fake Job Posts
Predicting Fake Job PostsPredicting Fake Job Posts
Predicting Fake Job PostsShibly Ahamed
 

Tendances (12)

Final ppt
Final pptFinal ppt
Final ppt
 
Gender and Age Detection using OpenCV.pptx
Gender and Age Detection using OpenCV.pptxGender and Age Detection using OpenCV.pptx
Gender and Age Detection using OpenCV.pptx
 
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHM
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHMHEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHM
HEART DISEASE PREDICTION USING NAIVE BAYES ALGORITHM
 
Multiple object detection report
Multiple object detection reportMultiple object detection report
Multiple object detection report
 
Iot and cloud computing on pervasive healthcare
Iot and cloud computing on pervasive healthcareIot and cloud computing on pervasive healthcare
Iot and cloud computing on pervasive healthcare
 
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...
4.Jan Holler, Vlasios Tsiatsis, Catherine Mulligan, Stefan Avesand, Stamatis ...
 
Ubiquitous computing
Ubiquitous computingUbiquitous computing
Ubiquitous computing
 
Sanja CV English
Sanja CV EnglishSanja CV English
Sanja CV English
 
Thermal Infrared Face Recognition
Thermal Infrared Face RecognitionThermal Infrared Face Recognition
Thermal Infrared Face Recognition
 
Cv issam elyaagoubi
Cv issam elyaagoubiCv issam elyaagoubi
Cv issam elyaagoubi
 
Reconnaissance d'ecriture manuscrite
Reconnaissance d'ecriture manuscriteReconnaissance d'ecriture manuscrite
Reconnaissance d'ecriture manuscrite
 
Predicting Fake Job Posts
Predicting Fake Job PostsPredicting Fake Job Posts
Predicting Fake Job Posts
 

En vedette

Algoritma floyd warshall dengan siklus negatif
Algoritma floyd warshall dengan siklus negatifAlgoritma floyd warshall dengan siklus negatif
Algoritma floyd warshall dengan siklus negatifBudi Raharjo
 
Dokumentasi crack wifi
Dokumentasi crack wifiDokumentasi crack wifi
Dokumentasi crack wifiBudi Raharjo
 
Green computing pil
Green computing  pilGreen computing  pil
Green computing pilBudi Raharjo
 
Tugas pdhupl kelompok flixel
Tugas pdhupl kelompok flixelTugas pdhupl kelompok flixel
Tugas pdhupl kelompok flixelBudi Raharjo
 
Analisa ud azam jaya
Analisa ud azam jayaAnalisa ud azam jaya
Analisa ud azam jayaBudi Raharjo
 
Instruksi mesin agus budi raharjo
Instruksi mesin agus budi raharjoInstruksi mesin agus budi raharjo
Instruksi mesin agus budi raharjoBudi Raharjo
 
Tugas 1 paal e agus budi raharjo 5109100164
Tugas 1 paal e agus budi raharjo 5109100164Tugas 1 paal e agus budi raharjo 5109100164
Tugas 1 paal e agus budi raharjo 5109100164Budi Raharjo
 
Process technology 5109100164 5109100702
Process technology 5109100164 5109100702Process technology 5109100164 5109100702
Process technology 5109100164 5109100702Budi Raharjo
 
Tugas framework j2 ee beda app session page
Tugas framework j2 ee beda app session pageTugas framework j2 ee beda app session page
Tugas framework j2 ee beda app session pageBudi Raharjo
 
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan annBudi Raharjo
 
Gl01 spec pl - bid me - 5112201905
Gl01 spec pl - bid me - 5112201905Gl01 spec pl - bid me - 5112201905
Gl01 spec pl - bid me - 5112201905Budi Raharjo
 

En vedette (15)

Algoritma floyd warshall dengan siklus negatif
Algoritma floyd warshall dengan siklus negatifAlgoritma floyd warshall dengan siklus negatif
Algoritma floyd warshall dengan siklus negatif
 
Dokumentasi crack wifi
Dokumentasi crack wifiDokumentasi crack wifi
Dokumentasi crack wifi
 
Green computing pil
Green computing  pilGreen computing  pil
Green computing pil
 
Tugas pdhupl kelompok flixel
Tugas pdhupl kelompok flixelTugas pdhupl kelompok flixel
Tugas pdhupl kelompok flixel
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Analisa ud azam jaya
Analisa ud azam jayaAnalisa ud azam jaya
Analisa ud azam jaya
 
Instruksi mesin agus budi raharjo
Instruksi mesin agus budi raharjoInstruksi mesin agus budi raharjo
Instruksi mesin agus budi raharjo
 
Presentasi kwn
Presentasi kwnPresentasi kwn
Presentasi kwn
 
Tugas 1 paal e agus budi raharjo 5109100164
Tugas 1 paal e agus budi raharjo 5109100164Tugas 1 paal e agus budi raharjo 5109100164
Tugas 1 paal e agus budi raharjo 5109100164
 
Process technology 5109100164 5109100702
Process technology 5109100164 5109100702Process technology 5109100164 5109100702
Process technology 5109100164 5109100702
 
Review game
Review gameReview game
Review game
 
Laporan topologi
Laporan topologiLaporan topologi
Laporan topologi
 
Tugas framework j2 ee beda app session page
Tugas framework j2 ee beda app session pageTugas framework j2 ee beda app session page
Tugas framework j2 ee beda app session page
 
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann
5112201905 memprediksi bidang minat mahasiswa menggunakan pca dan ann
 
Gl01 spec pl - bid me - 5112201905
Gl01 spec pl - bid me - 5112201905Gl01 spec pl - bid me - 5112201905
Gl01 spec pl - bid me - 5112201905
 

Similaire à Here are the key steps to solve this problem:1. Read the input which contains the length of the stick (L) and the number of pieces (N).2. Sort the cutting positions (lengths of pieces) in ascending order. This is important to minimize the total cost. 3. Initialize total cost to 0. 4. Iterate from the first cutting position to the last: - Calculate the length being cut off from the remaining stick. This is the difference between the current and previous cutting positions. - Add the cost of this cut to the total cost. The cost is equal to the length being cut off. - Update the remaining length of the stick.5. After the

4tocontest
4tocontest4tocontest
4tocontestberthin
 
Csr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydowCsr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydowCSR2011
 
LeastSquaresParameterEstimation.ppt
LeastSquaresParameterEstimation.pptLeastSquaresParameterEstimation.ppt
LeastSquaresParameterEstimation.pptStavrovDule2
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingPrudhviVuda
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanationsGopi Saiteja
 
Applications Section 1.3
Applications   Section 1.3Applications   Section 1.3
Applications Section 1.3mobart02
 
Aplicación de la serie Fourier en un circuito electrónico de potencia)
Aplicación de la serie Fourier en un circuito electrónico de potencia)Aplicación de la serie Fourier en un circuito electrónico de potencia)
Aplicación de la serie Fourier en un circuito electrónico de potencia)JOe Torres Palomino
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemVarad Meru
 
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...Simen Li
 
UNIT-II : SEQUENTIAL CIRCUIT DESIGN
UNIT-II  : SEQUENTIAL CIRCUIT DESIGN UNIT-II  : SEQUENTIAL CIRCUIT DESIGN
UNIT-II : SEQUENTIAL CIRCUIT DESIGN Dr.YNM
 
UNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGNUNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGNDr.YNM
 
Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01Sagar Gore
 
Digital Signal Processing Lab Manual ECE students
Digital Signal Processing Lab Manual ECE studentsDigital Signal Processing Lab Manual ECE students
Digital Signal Processing Lab Manual ECE studentsUR11EC098
 
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTERSYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTERTarun Kumar
 

Similaire à Here are the key steps to solve this problem:1. Read the input which contains the length of the stick (L) and the number of pieces (N).2. Sort the cutting positions (lengths of pieces) in ascending order. This is important to minimize the total cost. 3. Initialize total cost to 0. 4. Iterate from the first cutting position to the last: - Calculate the length being cut off from the remaining stick. This is the difference between the current and previous cutting positions. - Add the cost of this cut to the total cost. The cost is equal to the length being cut off. - Update the remaining length of the stick.5. After the (20)

4tocontest
4tocontest4tocontest
4tocontest
 
Brute force
Brute forceBrute force
Brute force
 
Csr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydowCsr2011 june15 12_00_davydow
Csr2011 june15 12_00_davydow
 
Signals and Systems Assignment Help
Signals and Systems Assignment HelpSignals and Systems Assignment Help
Signals and Systems Assignment Help
 
Composed short m sequences
Composed short m sequencesComposed short m sequences
Composed short m sequences
 
LeastSquaresParameterEstimation.ppt
LeastSquaresParameterEstimation.pptLeastSquaresParameterEstimation.ppt
LeastSquaresParameterEstimation.ppt
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Answers withexplanations
Answers withexplanationsAnswers withexplanations
Answers withexplanations
 
Applications Section 1.3
Applications   Section 1.3Applications   Section 1.3
Applications Section 1.3
 
Aplicación de la serie Fourier en un circuito electrónico de potencia)
Aplicación de la serie Fourier en un circuito electrónico de potencia)Aplicación de la serie Fourier en un circuito electrónico de potencia)
Aplicación de la serie Fourier en un circuito electrónico de potencia)
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction Problem
 
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...
Circuit Network Analysis - [Chapter5] Transfer function, frequency response, ...
 
UNIT-II : SEQUENTIAL CIRCUIT DESIGN
UNIT-II  : SEQUENTIAL CIRCUIT DESIGN UNIT-II  : SEQUENTIAL CIRCUIT DESIGN
UNIT-II : SEQUENTIAL CIRCUIT DESIGN
 
UNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGNUNIT-II -DIGITAL SYSTEM DESIGN
UNIT-II -DIGITAL SYSTEM DESIGN
 
Max net
Max netMax net
Max net
 
Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01Dsp 1recordprophess-140720055832-phpapp01
Dsp 1recordprophess-140720055832-phpapp01
 
Digital Signal Processing Lab Manual ECE students
Digital Signal Processing Lab Manual ECE studentsDigital Signal Processing Lab Manual ECE students
Digital Signal Processing Lab Manual ECE students
 
lecture4signals-181130200508.pptx
lecture4signals-181130200508.pptxlecture4signals-181130200508.pptx
lecture4signals-181130200508.pptx
 
2. signal flow
2. signal flow2. signal flow
2. signal flow
 
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTERSYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
SYSTEM IDENTIFICATION USING CEREBELLAR MODEL ARITHMETIC COMPUTER
 

Plus de Budi Raharjo

5112201905 house of quality
5112201905 house of quality5112201905 house of quality
5112201905 house of qualityBudi Raharjo
 
Penggunaan network address translation
Penggunaan network address translationPenggunaan network address translation
Penggunaan network address translationBudi Raharjo
 
Paper cloud computing br
Paper cloud computing brPaper cloud computing br
Paper cloud computing brBudi Raharjo
 
Protocol lan 5109100164
Protocol lan 5109100164Protocol lan 5109100164
Protocol lan 5109100164Budi Raharjo
 
Peranan pembelajaran elektronik
Peranan pembelajaran elektronikPeranan pembelajaran elektronik
Peranan pembelajaran elektronikBudi Raharjo
 
Perbedaan antar computer filesystem 5109100164
Perbedaan antar computer filesystem 5109100164Perbedaan antar computer filesystem 5109100164
Perbedaan antar computer filesystem 5109100164Budi Raharjo
 
Makalah pengantar basis data 5109100164
Makalah pengantar basis data 5109100164Makalah pengantar basis data 5109100164
Makalah pengantar basis data 5109100164Budi Raharjo
 
5109100023 makalah
5109100023 makalah5109100023 makalah
5109100023 makalahBudi Raharjo
 

Plus de Budi Raharjo (10)

5112201905 house of quality
5112201905 house of quality5112201905 house of quality
5112201905 house of quality
 
Penggunaan network address translation
Penggunaan network address translationPenggunaan network address translation
Penggunaan network address translation
 
Paper cloud computing br
Paper cloud computing brPaper cloud computing br
Paper cloud computing br
 
Protocol lan 5109100164
Protocol lan 5109100164Protocol lan 5109100164
Protocol lan 5109100164
 
Peranan pembelajaran elektronik
Peranan pembelajaran elektronikPeranan pembelajaran elektronik
Peranan pembelajaran elektronik
 
Perbedaan antar computer filesystem 5109100164
Perbedaan antar computer filesystem 5109100164Perbedaan antar computer filesystem 5109100164
Perbedaan antar computer filesystem 5109100164
 
Makalah pengantar basis data 5109100164
Makalah pengantar basis data 5109100164Makalah pengantar basis data 5109100164
Makalah pengantar basis data 5109100164
 
Agama
AgamaAgama
Agama
 
5109100023 makalah
5109100023 makalah5109100023 makalah
5109100023 makalah
 
desain server
desain serverdesain server
desain server
 

Dernier

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Dernier (20)

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

Here are the key steps to solve this problem:1. Read the input which contains the length of the stick (L) and the number of pieces (N).2. Sort the cutting positions (lengths of pieces) in ascending order. This is important to minimize the total cost. 3. Initialize total cost to 0. 4. Iterate from the first cutting position to the last: - Calculate the length being cut off from the remaining stick. This is the difference between the current and previous cutting positions. - Add the cost of this cut to the total cost. The cost is equal to the length being cut off. - Update the remaining length of the stick.5. After the

  • 1. Perancangan dan Analisis Algoritme Lanjut Agus Budi Raharjo 5109100164 Jurusan Teknik Informatika Fakultas Teknologi Informasi Institut Teknologi Sepuluh Nopember
  • 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. 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. UVA 10069 – Distinct Subsequences Sample Input 2 babgbag bag rabbbit rabbit Sample Output 5 3 __________________________________________________________________________________ Rezaul Alam Chowdhury
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. UVA 10003 – Cutting Sticks
  • 20. UVA 10003 – Cutting Sticks
  • 21. UVA 10003 – Cutting Sticks
  • 22. UVA 10003 – Cutting Sticks
  • 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. 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; }