移动最小二乘_最小移动以形成弦
移動最小二乘
Problem statement:
問題陳述:
Given a string S, write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step, you can:
給定字符串S ,編寫一個程序以檢查是否可以通過多次執行以下任何操作來構造給定的字符串S。 在每個步驟中,您可以:
Add any character at the end of the string.
在字符串末尾添加任何字符。
Append the existing string to the string itself.
將現有字符串追加到字符串本身。
The above steps can be applied any number of times. We need to print the minimum steps required to form the string.
以上步驟可以應用多次。 我們需要打印形成字符串所需的最少步驟。
Input:
輸入:
Input String
輸入字符串
Output:
輸出:
Minimum number of steps required to form that string.
形成該字符串所需的最少步驟數。
Constraints:
限制條件:
1 <= string length <= 10^5Example:
例:
Input: aaaaaaaa Output: 4Explanation: Move 1: add 'a' to form "a" Move 2: add 'a' to form "aa" Move 3: append "aa" to form "aaaa" Move 4: append "aaaa" to form "aaaaaaaa"Input: ijhijhiOutput: 5Explanation: Move 1: add 'i' to form "i" Move 2: add 'j' to form "ij" Move 3: add 'h' to form "ijh" Move 4: append "ijh" to form "ijhijh" Move 5: add 'i' to form "ijhijhi"Solution Approach:
解決方法:
So, we have two option
因此,我們有兩種選擇
Append single character
附加單個字符
Append the string itself
附加字符串本身
So, say the string is
所以說這個字符串是
x1x2 ... xnFor any sub-problem of size i,
對于任何大小為i的子問題,
x1x2 ... xiTwo things are possible,
有兩種可能
Appends xi to sub-problem x1x2 ... x(i-1)
將x i附加到子問題x 1 x 2 ... x (i-1)
Append x1..xj to x1..xj is x(j+1)..xi is similar to x1..xj
將x 1 ..x j追加到x 1 ..x j是x (j + 1) ..x i類似于x 1 ..x j
So, we can formulate the problem recursively,
因此,我們可以遞歸地提出問題,
f(i) = minimum steps to form x1x2 ... xi f(i) = f(i-1)+1 f(i) = f(j)+1 if j=i/2 and the partitioned strings are sameNow find the minimum.
現在找到最小值。
The base value of f(i)=i obviously which is maximum value to form the string.
f(i)= i的基值顯然是構成字符串的最大值。
So, the above recursion can be transformed to DP.
因此,上述遞歸可以轉換為DP。
1) Declare int dp[n+1]; 2) Assign the base value. for i=0 to ndp[i]=i; 3) Fill the DP arrayfor i=2 to ndp[i]=minimum(dp[i-1]+1,dp[i]); // normal insertion, recursion first caseif i is evenif s1s(i/2) = s(i/2+1)sn // if appending string is possibledp[i]=minimum(dp[i],dp[i/2]+1);end ifend for4) return dp[n];DP[n] is the final resultC++ Implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;int minimumMoves(string s) {int n = s.length();int dp[n + 1];for (int i = 0; i <= n; i++)dp[i] = i;for (int i = 2; i <= n; i++) {dp[i] = std::min(dp[i - 1] + 1, dp[i]);if (i % 2 == 0) {int flag = 0;for (int j = 0; j < i / 2; j++) {if (s[j] != s[j + i / 2]) {flag = 1;break;}}if (flag == 0)dp[i] = std::min(dp[i], dp[i / 2] + 1);}}return dp[n]; }int main() {cout << "Enter input string\n";string s;cin >> s;cout << "Minimum steps to form te string: " << minimumMoves(s) << endl;return 0; }Output:
輸出:
Enter input string incinclude Minimum steps to form te string: 8翻譯自: https://www.includehelp.com/icp/minimal-moves-to-form-a-string.aspx
移動最小二乘
總結
以上是生活随笔為你收集整理的移动最小二乘_最小移动以形成弦的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Hashtable equal
- 下一篇: java 获取service_Java