Topcoder SRM 497 DIV2 1000 MakeSquare

题意:给你一个字符串,对这个字符串有三种操作。

1)任意位置插入任意一个字符

2)任意位置删除一个字符

3)改变任意位置的一个字符。

问你最后要经过最少的步数使得这个字符串由两个相同的字符串串联构成。

解题思路:枚举将两个字符串分成两个串,然后对这两个串求改变k(0-len)次的最长公共子序列即可。

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "MakeSquare.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31 
 32 #define PB push_back
 33 #define MP make_pair
 34 
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38 
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44 
 45 char str[1000];
 46 int len ; 
 47 int dp[200][200][70];
 48 int solve(int x)
 49 {
 50   memset(dp,0,sizeof(dp));
 51   int same = 0 ; 
 52   for(int i = 1;i <= x ;i ++)
 53       for(int j = x+1 ;j <= len ;j ++)
 54       {
 55           same = 0 ; 
 56          if(str[i] == str[j]) 
 57          {
 58             same = 1;
 59          }
 60         if(same == 0 )
 61              for(int k = 0 ; k <= len ; k ++)
 62              {
 63                  dp[i][j][k+1] = max(dp[i][j][k+1],dp[i-1][j-1][k] + 1);
 64              }
 65 
 66            for(int k = 0 ; k <= len; k ++)
 67              {
 68                  dp[i][j][k] = max(dp[i][j][k],dp[i-1][j-1][k] + same);
 69                  dp[i][j][k] = max(dp[i][j][k],dp[i][j-1][k] );
 70                  dp[i][j][k] = max(dp[i][j][k],dp[i-1][j][k] );
 71              }
 72       }
 73   int len1 = x;
 74   int len2 = len - x;
 75   //printf("%d %d %d\n",len1,len2,dp[x][len]);
 76   int ans = len;
 77   for(int k = 0; k <= len; k ++)
 78   {
 79     ans = min(ans,len1 - dp[x][len][k] + len2 - dp[x][len][k] + k);
 80   }
 81   return ans;
 82 }
 83 class MakeSquare
 84 {
 85         public:
 86         int minChanges(string S)
 87         {
 88            len = S.size();
 89            int ans = len;
 90            for(int i = 0 ;i < len; i ++)
 91                str[i+1] = S[i];
 92            for(int i = 1;i < len;i ++)
 93                 ans = min(ans,solve(i));
 94            return ans;
 95         }
 96         
 97 // BEGIN CUT HERE
 98     public:
 99     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
100     private:
101     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
102     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
103     void test_case_0() { string Arg0 = "abcdabgcd"; int Arg1 = 1; verify_case(0, Arg1, minChanges(Arg0)); }
104     void test_case_1() { string Arg0 = "abcdeabce"; int Arg1 = 1; verify_case(1, Arg1, minChanges(Arg0)); }
105     void test_case_2() { string Arg0 = "abcdeabxde"; int Arg1 = 1; verify_case(2, Arg1, minChanges(Arg0)); }
106     void test_case_3() { string Arg0 = "aabcaabc"; int Arg1 = 0; verify_case(3, Arg1, minChanges(Arg0)); }
107     void test_case_4() { string Arg0 = "aaaaabaaaaabaaaaa"; int Arg1 = 2; verify_case(4, Arg1, minChanges(Arg0)); }
108 
109 // END CUT HERE
110 
111 };
112 
113 // BEGIN CUT HERE
114 int main()
115 {
116         MakeSquare ___test;
117         ___test.run_test(-1);
118         return 0;
119 }
120 // END CUT HERE
View Code

 

转载于:https://www.cnblogs.com/zyue/p/4361301.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用 JavaScript 编写的杀死幽灵游戏(附源代码) 杀死鬼魂游戏是使用 Vanilla JavaScript、CSS 和 HTML 画布开发的简单项目。这款游戏很有趣。玩家必须触摸/杀死游荡的鬼魂才能得分。您必须将鼠标悬停在鬼魂上 - 尽量得分。鬼魂在眨眼间不断从一个地方移动到另一个地方。您必须在 1 分钟内尽可能多地杀死鬼魂。 游戏制作 这个游戏项目只是用 HTML 画布、CSS 和 JavaScript 编写的。说到这个游戏的特点,用户必须触摸/杀死游荡的幽灵才能得分。游戏会根据你杀死的幽灵数量来记录你的总分。你必须将鼠标悬停在幽灵上——尽量得分。你必须在 1 分钟内尽可能多地杀死幽灵。游戏还会显示最高排名分数,如果你成功击败它,该分数会在游戏结束屏幕上更新。 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox。要玩游戏,首先,单击 index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值