1015. Letter-moving Game ( time limit) ms
Memory Limit 65536 KB
Code length limit 8000 B
Standard author CAO, Peng
Here are a simple intersting letter-moving game. The game starts with 2 strings S and T consist of lower case Chinese letters. S and T contain the same letters but the orders might be different. In the other words S can is obtained by the shuffling letters in String T. At each step, you can move one arbitrary letter in S either to the beginning or to the "end of". How many steps in least to change S into T?
Input Specification:
Each input file contains one test case. For each case, the the "contains" string S, and the second line contains the string T. They consist of the lower case 中文版 letters and S can be obtained by shuffling T ' s letters. The length of S is no larger than 1000.
Output Specification:
For each case, print in a line the least number of steps to change S into T in the game. Sample Input:
IONONMROGDG
goodmorning
Sample Output:
8
Sample Solution:
(0) starts from IONONMROGDG (1) Move "The last G to"
beginning:giononmrogd
(2) Move M to the end:giononrogdm
(3) Move the "the" the "End:ginonrogdmo"
(4) Move R to the End:ginonogdmor
(5) Move the "the" D:gionogdmorn (6) Move I to the "End:gonogdmorni" (7) Move the "the", "the", "
end:googdmornin
(8) Move the" Second g to the end:goodmorning
Because you can put letters to the front and back, then the LCS will ensure that the pattern string matches a continuous string of strings of text
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010;
int dp[n][n], a[n];
Char S1[n], s2[n];
int main ()
{
memset (dp,0,sizeof (DP));
scanf ("%s%s", s2+1, s1+1);
int L1=strlen (s1+1), L2=strlen (s2+1);
for (int i=1;i<=l1;i++) a[i]=l1+1;
int ans=0;
for (int i=1;i<=l1;i++)
{for
(int j=1;j<=l2;j++)
{
if (s1[i]==s2[j]&&j>a[i-1) )
dp[i][j]=dp[i-1][j-1]+1,a[i]=min (a[i],j);
else if (S1[i]==s2[j])
dp[i][j]=1,a[i]=min (a[i],j);
else dp[i][j]=dp[i][j-1];
Ans=max (Ans,dp[i][j]);
}
printf ("%d\n", L1-ans);
return 0;
}