UVA-11127 triple-free Binary Strings Dfs

Source: Internet
Author: User

The main topic: there is a string, the string is composed of 0, 1 and *, * can be changed to 0 can also be changed to 1, the character required to change the string can only be 0 or 1.
This string is now given, requiring that no three consecutive identical substrings should be present in the changed string, and that the maximum number of strings that can be changed to match the rules

Thinking: Because only 0 and 1, and only 30 bits, so you can use a positive number to represent the state. DFS brute force enumeration, edge enumeration edge judgment, see if current matches

#include <cstdio>#include <cstring>using namespace Std;#define MAXNintNChar Str[Maxn];bool Judge (intSintL) {int MoD= (1<< L)-1;intFirst = (S &MoD); s = (S & (~MoD)) >> L;intSecond = (S &MoD); s = (S & (~MoD)) >> L;if(first = = Second && second = = s)return true;return false;}intDfsintSintcur) {intS0 = s; for(inti =0; I <= cur-3; i++) {if(((cur-i)%3==0) && judge (S0, (cur-i)/3))return 0; S0 = (S0 & (~1)) >>1; }if(cur = = N)return 1;if(Str[cur] = =' 0 ')returnDFS (s, cur +1);if(Str[cur] = =' 1 ')returnDFS (s ^ (1<< cur), cur +1);returnDFS (s, cur +1) + DFS (s ^ (1<< cur), cur +1);}intMain () {intCAS =1; while(SCANF ("%d", &n)! = EOF && N) {scanf ('%s ',Str); printf"Case%d:%d\n", cas++, DFS (0,0)); }return 0;}

UVA-11127 triple-free Binary Strings Dfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.