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
The document discusses the cutting sticks problem where a stick of length L needs to be cut into pieces at given cut locations. The optimal solution is to find the cutting order that minimizes the total cost, where the cost of each piece is its length. The problem is solved using dynamic programming by filling a 2D table S where S[i][j] stores the minimum cost of cutting the stick between indexes i and j. The table is filled in increasing order of substring length from 2 to N+1, considering all possible cut locations between i and j. The minimum cost is stored in S[0][N].
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
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)
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.
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.
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
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