zoukankan      html  css  js  c++  java
  • Letter-moving Game

    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:

    iononmrogdg
    goodmorning
    
     

    Sample Output:

    8
    
     

    Sample 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
     1 #include<bits/stdc++.h>
     2 #include<ext/pb_ds/assoc_container.hpp>
     3 #include<ext/pb_ds/tree_policy.hpp>
     4 using namespace __gnu_pbds;
     5 using namespace std;
     6 int main()
     7 {
     8 //    freopen("data.txt","r",stdin);
     9     ios::sync_with_stdio(false);
    10     int n,mk=0;
    11     string s1,s2;
    12     cin>>s1>>s2;
    13     for(int i=0;i<s2.size();i++)
    14     {
    15         int k=0,ii=i;
    16         for(auto j:s1)
    17         {
    18             if(s2[ii]==j)
    19             {
    20                 ii++;
    21                 k++;
    22             }
    23         }
    24         mk=max(k,mk);
    25     }
    26     cout<<s1.size()-mk;
    27     return 0;
    28 }
    诚者,君子之所守也。
  • 相关阅读:
    算法面试题总结
    面试题目整理
    九月百度,迅雷,华为,阿里巴巴,最新校招笔试面试十题
    ubuntu 环境变量配置
    VM 共享设置
    五大常用算法之五:分支限界法
    五大常用算法之四:回溯法
    Python之路【第十六篇】Django基础
    Python之路【第十五篇】WEB框架
    Python之路【第十四篇】前端补充回顾
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285785.html
Copyright © 2011-2022 走看看