【动态规划】 TopCoder SRM 555 CuttingBitString
2012-10-15 22:35
253 查看
大致意思就是把一个01串切分成一系列是5的幂次方的子串,题意叙述简洁,是一道很好的动态规划题目。
我的源代码:
离线测试了一下,应该是对的,具体大家如果感兴趣可以到TopCoder的网站上去看,非常详细(这是我觉得TopCoder与传统OJ相比的很大优势了……)
We are in a distant future. After the downfall of mankind, the Earth is now ruled by fairies. The "Turing game Online" website is hot among fairies right now. On this website, everyone can play the programming puzzle "Turing game". Fairies love powers of 5, that is, the numbers 1, 5, 25, 125, 625, and so on. In the Turing game, the player is given a string of bits (zeros and ones). The ideal situation is when the string is represents a power of 5 in binary, with no leading zeros. If that is not the case, the fairy player tries to cut the given string into pieces, each piece being a binary representation of a power of 5, with no leading zeros. Of course, it may be the case that even this is impossible. In that case, the fairy player becomes depressed, and bad things happen when a fairy gets depressed. You, as one of the surviving humans, are in charge of checking the bit strings to prevent the bad things from happening. You are given a String S that consists of characters '0' and '1' only. S represents the string given to a player of the Turing game. Return the smallest positive integer K such that it is possible to cut S into K pieces, each of them being a power of 5. If there is no such K, return -1 instead. |
#include<string> #include<vector> #include<algorithm> #define MAXIMUM 99999 using namespace std; class CuttingBitString{ public: //get min using dynamic programming method int getmin(string s){ int len=s.size(); int goal[60]; for(int i=0;i<len;i++){ goal[i]=MAXIMUM; } for(int i=0;i<len;i++){ //additional test if(isFit(s,0,i)==true){ // 1 will be the minimum number goal[i]=1; continue; } for(int j=0;j<i;j++){ //right substring is not power of 5 if(isFit(s,j+1,i)==false) continue; ///front substring is not power of 5 if(goal[j]==MAXIMUM) continue; goal[i]=min(goal[i],goal[j]+1); } }//end external for loop //there is no solution if(goal[len-1]==MAXIMUM) return -1; return goal[len-1]; } //return if this substring is the power of 5 bool isFit(string s,int begin,int end){ if(begin>end) return false; //long long data type is enough for this problem long long counter=0; for(int i=begin;i<=end;i++){ if(s[i]=='0') counter=counter<<1; else if(s[i]=='1') counter=(counter<<1)+1; } if(counter==0) return false; //test whether it is the power of 5 while(counter%5==0){ counter=counter/5; } //the power of five will lead to 1 in the end if(counter==1) return true; return false; } };
离线测试了一下,应该是对的,具体大家如果感兴趣可以到TopCoder的网站上去看,非常详细(这是我觉得TopCoder与传统OJ相比的很大优势了……)
相关文章推荐
- 【动态规划】TopCoder SRM 573 division2 WolfPackDivTwo
- 【topcoder SRM 652 DIV2 250】ValueOfString
- topcoder-SRM565-div2-第二题-500分--搜索/动态规划
- Topcoder Arena SRM 144 DIV2 1100 point 图遍历(非递归)+动态规划
- Topcoder SRM 687 (Div 2) 250.Quorum __ sort
- leetcode 87 Scramble String(动态规划)
- 字符串最短距离CalculateStringDistance(递归 or 动态规划)
- topcoder_SRM_615_250
- TopCoder SRM 652 Div2 Problem 1000 - NoRightTurnDiv2 (几何 + 贪心)
- SRM556 Div1 555
- Topcoder SRM 514div2 500point
- [TopCoder] SRM_594_DIV2.250
- Topcoder SRM 152 Div2 1000(状态压缩呀)
- topcoder srm 677 DIV 1 300
- topcoder srm 310 div1
- topcoder SRM 548 DIV2 500
- topcoder srm 696 div1 -3
- TopCoder SRM 657 Div2 Problem 500 - ProblemSetsEasy (二分 + 模拟)
- Topcoder SRM 583 DIV2 SwappingDigits
- topcoder srm 600 div1