日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

移动最小二乘_最小移动以形成弦

發布時間:2025/3/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 移动最小二乘_最小移动以形成弦 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

移動最小二乘

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^5

    Example:

    例:

    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 ... xn

    For any sub-problem of size i,

    對于任何大小為i的子問題,

    x1x2 ... xi

    Two 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 same

    Now 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 result

    C++ 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

    移動最小二乘

    總結

    以上是生活随笔為你收集整理的移动最小二乘_最小移动以形成弦的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。