pat 1015. Letter-moving Game (35)(LCS)
来源:互联网 发布:mac eclipse new task 编辑:程序博客网 时间:2024/06/05 05:35
1015. Letter-moving Game (35)
Here is a simple intersting letter-moving game. The game starts with 2 strings S and T consist of lower case English letters. S and T contain the same letters but the orders might be different. In other words S can be obtained by 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 it. How many steps at least to change S into T?
Input Specification:
Each input file contains one test case. For each case, the first line contains the string S, and the second line contains the string T. They consist of only the lower case English 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:iononmrogdggoodmorningSample Output:
8Sample Solution:
(0) starts from iononmrogdg(1) Move the last g to the beginning: giononmrogd(2) Move m to the end: giononrogdm(3) Move the first o to the end: ginonrogdmo(4) Move r to the end: ginonogdmor(5) Move the first n to the end: gionogdmorn(6) Move i to the end: gonogdmorni(7) Move the first n to the end: googdmornin(8) Move the second g to the end: goodmorning
#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;}
- pat 1015. Letter-moving Game (35)(LCS)
- pat-top 1015. Letter-moving Game (35)
- PAT (Top Level) Practise 1015. Letter-moving Game (35)
- 1015. Letter-moving Game (35)
- PAT1015: Letter-moving Game(LCS)
- 1015. Letter-moving Game (35)解题报告
- Letter-moving Game 1月8日
- Letter Game
- soj 2796 Letter Deletion LCS
- poj 1171 Letter Game
- usaco letter game
- [USACO4.3.3]Letter Game
- usaco 4.3 Letter Game 搜索
- usaco 4.3 Letter Game 搜索
- USACO Section 4.3 Letter Game
- usaco 4.3.4 letter game
- USACO Section 4.3 Letter Game
- usaco 4.3.4 Letter Game
- Centos7下mysql5.7社区版安装步骤
- 张正友相机标定Opencv实现以及标定流程&&标定结果评价&&图像矫正流程解析(附标定程序和棋盘图)
- 自然语言处理入门——自然语言处理的背景、应用、推荐资料
- Mac的xgboost安装
- Spring MVC 遇到的问题集合
- pat 1015. Letter-moving Game (35)(LCS)
- Java编程思想第四版第六章练习
- 零起点学算法96——折线分割平面(找规律)
- EXCEL VBA笔记(一)
- 素数判断
- 解决ssh连接一段时间后光标无响应问题
- 一步步实现WebServer中间件——项目介绍
- USB描述符 包括bushound抓包
- USB描述符和枚举总结 --自己总结