SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
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

Weitere Àhnliche Inhalte

Was ist angesagt?

wireless sensor networks & application :forest fire detection
 wireless sensor networks  & application :forest fire detection wireless sensor networks  & application :forest fire detection
wireless sensor networks & application :forest fire detectionMueenudheen Shafaquath V P
 
Advanced Pipelining in ARM Processors.pptx
Advanced Pipelining  in ARM Processors.pptxAdvanced Pipelining  in ARM Processors.pptx
Advanced Pipelining in ARM Processors.pptxJoyChowdhury30
 
Wireless sensor network report
Wireless sensor network reportWireless sensor network report
Wireless sensor network reportGanesh Khadsan
 
Smart Home Using IOT simulation In cisco packet tracer
Smart Home Using IOT simulation In cisco packet tracerSmart Home Using IOT simulation In cisco packet tracer
Smart Home Using IOT simulation In cisco packet tracerKhyathiNandankumar
 
Wireless Sensor Network
Wireless Sensor NetworkWireless Sensor Network
Wireless Sensor NetworkGanesh Khadsan
 
SWITCHING SYSTEM SOFTWARE
SWITCHING SYSTEM SOFTWARESWITCHING SYSTEM SOFTWARE
SWITCHING SYSTEM SOFTWAREASFIASULTANA4
 
Wireless human health Monitor
Wireless human health MonitorWireless human health Monitor
Wireless human health MonitorAmarendra K Yadav
 
embedded system and iot.pptx
embedded system and iot.pptxembedded system and iot.pptx
embedded system and iot.pptxSanjanaN25
 
Nano computing.
Nano computing.Nano computing.
Nano computing.Sunny Sundeep
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networksAneeshGKumar
 
Wireless sensor network and its application
Wireless sensor network and its applicationWireless sensor network and its application
Wireless sensor network and its applicationRoma Vyas
 
Network security 10EC832 vtu notes
Network security 10EC832 vtu notesNetwork security 10EC832 vtu notes
Network security 10EC832 vtu notesJayanth Dwijesh H P
 
Smart retail using IOT
Smart retail using IOTSmart retail using IOT
Smart retail using IOTKUNAL RANA
 
Project report on arduino based parking lot system
Project report on arduino based parking lot systemProject report on arduino based parking lot system
Project report on arduino based parking lot systemUnited International University
 

Was ist angesagt? (20)

Introduction of iot
Introduction of iotIntroduction of iot
Introduction of iot
 
wireless sensor networks & application :forest fire detection
 wireless sensor networks  & application :forest fire detection wireless sensor networks  & application :forest fire detection
wireless sensor networks & application :forest fire detection
 
Advanced Pipelining in ARM Processors.pptx
Advanced Pipelining  in ARM Processors.pptxAdvanced Pipelining  in ARM Processors.pptx
Advanced Pipelining in ARM Processors.pptx
 
Wireless sensor network report
Wireless sensor network reportWireless sensor network report
Wireless sensor network report
 
Zigbee
ZigbeeZigbee
Zigbee
 
Smart Home Using IOT simulation In cisco packet tracer
Smart Home Using IOT simulation In cisco packet tracerSmart Home Using IOT simulation In cisco packet tracer
Smart Home Using IOT simulation In cisco packet tracer
 
Wireless Sensor Network
Wireless Sensor NetworkWireless Sensor Network
Wireless Sensor Network
 
SWITCHING SYSTEM SOFTWARE
SWITCHING SYSTEM SOFTWARESWITCHING SYSTEM SOFTWARE
SWITCHING SYSTEM SOFTWARE
 
Wireless human health Monitor
Wireless human health MonitorWireless human health Monitor
Wireless human health Monitor
 
embedded system and iot.pptx
embedded system and iot.pptxembedded system and iot.pptx
embedded system and iot.pptx
 
Nano computing.
Nano computing.Nano computing.
Nano computing.
 
Mobile Number Portability ppt
Mobile Number Portability pptMobile Number Portability ppt
Mobile Number Portability ppt
 
IOT BASED SMART AGRICULTURE
IOT BASED SMART AGRICULTUREIOT BASED SMART AGRICULTURE
IOT BASED SMART AGRICULTURE
 
Barcode technology
Barcode technologyBarcode technology
Barcode technology
 
IT6601 MOBILE COMPUTING
IT6601 MOBILE COMPUTINGIT6601 MOBILE COMPUTING
IT6601 MOBILE COMPUTING
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networks
 
Wireless sensor network and its application
Wireless sensor network and its applicationWireless sensor network and its application
Wireless sensor network and its application
 
Network security 10EC832 vtu notes
Network security 10EC832 vtu notesNetwork security 10EC832 vtu notes
Network security 10EC832 vtu notes
 
Smart retail using IOT
Smart retail using IOTSmart retail using IOT
Smart retail using IOT
 
Project report on arduino based parking lot system
Project report on arduino based parking lot systemProject report on arduino based parking lot system
Project report on arduino based parking lot system
 

Andere mochten auch

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
 
Cloud computing
Cloud computingCloud computing
Cloud computingBudi 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
 
Presentasi kwn
Presentasi kwnPresentasi kwn
Presentasi kwnBudi 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
 
Laporan topologi
Laporan topologiLaporan topologi
Laporan topologiBudi 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
 

Andere mochten auch (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
 

Ähnlich wie 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
 
Composed short m sequences
Composed short m sequencesComposed short m sequences
Composed short m sequencesIAEME Publication
 
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
 
lecture4signals-181130200508.pptx
lecture4signals-181130200508.pptxlecture4signals-181130200508.pptx
lecture4signals-181130200508.pptxRockFellerSinghRusse
 
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
 

Ähnlich wie 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
 

Mehr von 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
 
desain server
desain serverdesain server
desain serverBudi Raharjo
 

Mehr von 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
 

KĂŒrzlich hochgeladen

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

KĂŒrzlich hochgeladen (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

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; }