#FW0000082. Đổi xâu theo đoạn chi phí nhỏ nhất II (Minimum Cost to Convert String II)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đổi xâu theo đoạn chi phí nhỏ nhất II (Minimum Cost to Convert String II)
Minimum Cost to Convert String II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given equal-length source and target strings, each rule converts one contiguous substring to another substring of equal length at a positive cost. Multiple conversions may be chained on the same segment, while distinct converted segments must not overlap. Unchanged characters cost zero. Minimize total cost.
Input
First , then lines of original replacement cost. Each rule maps strings of equal length.
Output
Print the minimum total cost or -1 if impossible.
Subtasks
-
Subtask 1 (40 points): . Chỉ dùng chữ thường
a–z; hai chuỗi của mỗi quy tắc bằng độ dài, tối đa ký tự trong bộ PHOJ; . -
Subtask 2 (60 points): . Chỉ dùng chữ thường
a–z; hai chuỗi của mỗi quy tắc bằng độ dài, tối đa ký tự trong bộ PHOJ; .
Examples
Input
abcd
wxyz
4
ab wx 3
cd yz 2
a w 10
b x 10
Output
5
Explanation
Convert ab to wx for cost 3 and cd to yz for cost 2. These segments do not overlap, giving minimum total cost 5.