Editing
2020 Jan Bronze Problem 1 Word Processor
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Official Problem Statement == [http://www.usaco.org/index.php?page=viewproblem2&cpid=987 Word Processor] Problem Statement: Given a text document of length N, you must determine the minimum number of keystrokes needed to type the document. Solution: The solution to this problem is to use a dynamic programming approach. We can create a two-dimensional array of size N+1 x N+1, where the first row and column represent the empty string. We then fill the array with the minimum number of keystrokes needed to type the substring from the beginning of the document to the character at position (i, j). For example, if the document is "abcdef", the array would look like this: | | | a | b | c | d | e | f | |---|---|---|---|---|---|---|---| | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | | a | 0 | 0 | 1 | 2 | 3 | 4 | 5 | | b | 0 | 0 | 0 | 1 | 2 | 3 | 4 | | c | 0 | 0 | 0 | 0 | 1 | 2 | 3 | | d | 0 | 0 | 0 | 0 | 0 | 1 | 2 | | e | 0 | 0 | 0 | 0 | 0 | 0 | 1 | | f | 0 | 0 | 0 | 0 | 0 | 0 | 0 | We then use the following recurrence relation to fill the array: dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + 1) for all k in [i, j-1] where dp[i][j] is the minimum number of keystrokes needed to type the substring from the beginning of the document to the character at position (i, j). We can then use this array to determine the minimum number of keystrokes needed to type the entire document. The answer is dp[0][N], which is the value in the bottom right corner of the array. C++ Code: #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string s; cin >> s; int dp[N+1][N+1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { dp[i][j] = j - i; for (int k = i; k < j; k++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + 1); } } } cout << dp[0][N] << endl; return 0; } [[Category:Yearly_2019_2020]] [[Category:Bronze]] [[Category:Simulation]] [[Category:String Manipulation]] [[Category:Greedy]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information