Palindromic changes
Practice
4 (11 votes)
Algorithms
Dynamic programming
Floyd warshall algorithm
Graphs
Shortest path algorithm
String manipulation
Problem
25% Success 2078 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code
You are given the cost of changing \(M\) pairs of lowercase characters \(x\) to \(y\). You are also given a string \(S\).
You can change a character in string \(S\) to another lowercase character by spending the cost you were given earlier. Determine the minimum cost that is required to make \(S\) a palindrome.
You can assume that it is always possible to make S a palindrome.
Notes
- Each pair of characters is there at most 1 time in the input.
- A string \(S\) is called a palindrome if it reads the same if it is read backwards. For example, 'radar', 'madam', 'racecar' are palindromes whereas 'pizza' is not.
Input format
- The first line contains string \(S\).
- The next line contains integer \(M\).
- The next \(M\) lines each contain a pair of lowercase characters \(x\), \(y\), and cost \(c\). You can change character \(x\) to character \(y\) or vise versa spending cost \(c\).
Output format
Print the minimum cost that is required to make \(S\) a palindrome.
Constraints
\(1 \le |S| \le 10^5\)
\(0 \le M \le 325\)
\(1 \le C_i \le 10^8\) where \(C_i\) is the cost of changing the \(i^{th}\) pair of characters
Submissions
Please login to view your submissions
Similar Problems
Points:30
14 votes
Tags:
AlgorithmsDijkstra's AlgorithmEasyGraphsShortest Path Algorithm
Points:30
6 votes
Tags:
AlgorithmsApprovedDijkstra's algorithmEasyGraphsHiringRecruitShortest Path Algorithmapproved
Points:30
6 votes
Tags:
Depth First SearchEasyFloyd Warshall AlgorithmGraphs
Editorial