Algorithm Training Cowboys (DP)

Source: Internet
Author: User

The question describes a hairbreadth moment: N Cowboys standing in a ring, and each cowboy pointed to the man next to him with a revolver! Each cowboy points to his neighbor in a clockwise or counterclockwise direction. As many westerns do, at this moment, the rope is the thorn of the No pity ... The scene of confrontation is changing every second. Every second the Cowboys analyzed the situation, and when a pair of neighboring Cowboys found that they were pointing at each other, they turned around. Every pair of these cowboys will turn around in a second. All the turns are taking place in a flash. We use letters to denote the direction the cowboy is pointing. "A" indicates a clockwise direction, and "B" indicates a counterclockwise direction. Thus, a string containing only "a" and "B" is used to denote the ring made of the cowboy. This is the first record to be made by a cowboy pointing to the clockwise direction. For example, the cowboy ring "Abbbabbba" will become "Babbbabba" in one second, while the cowboy Ring "Babba" will become "Ababb". This picture shows how "Babba" became "Ababb" a second later, and now uses the string s to denote the denim's arrangement. Your task is to find out how many possible permutations of a second ago. If a cowboy in an arrangement points clockwise, and in another arrangement he points counterclockwise, the two permutations are different. Input format input data includes a string s, which contains only "a" and "B". Output format output the number of possible permutations you have calculated a second ago. Data size and convention s of length 3 to 100 (including 3 and 100) sample input Babbbabba sample output 2 sample input Ababb sample output 2 sample input Ababab Sample output 4 Sample Description Test Example One, the possible initial arrangement is: "Abbbabbab" and " Abbbabbba ".
In the test sample two, the possible initial permutations are: "AABBB" and "Babba".

Exercises

#include <bits/stdc++.h>using namespacestd;Long Longdp[Wuyi][4]={0};intMain () {dp[1][0]=1; dp[1][1]=1; dp[1][2]=2; dp[1][3]=1;  for(intI=2; i<= -; i++) {//hit table DP condition numberdp[i][0]=dp[i-1][2]; dp[i][1]=dp[i-1][3]; dp[i][2]=dp[i-1][0]+dp[i-1][2]; dp[i][3]=dp[i-1][2]; }    stringTarget; intLength,i;  while(cin>>target) {Length=target.size (); stringtmp1="", tmp2="";//Check for the existence of a full A or full B condition         for(intk=0; i<length;k++) {TMP1+="A"; TMP2+="B"; }        if(target==tmp1| | target==TMP2) {cout<<0<<Endl; return 0; }        if(length%2==0) {TMP1=""; TMP2="";  for(intI=1; i<=length/2; i++) {//Check if the whole string is a BA string! tmp1+="AB"; TMP2+="BA"; }            if(tmp1==target| | tmp2==target) {cout<<dp[length/2-1][1]+dp[length/2-1][2]<<Endl; return 0; }        }         for(i=0; i<length;i++) {//find the first set of BA start positions            if(target[i]=='B'&&target[(i+1)%length]=='A'){                 Break; }        }         for(intJ=i;; J= (j+2) {%length) {//find the first set of BA end positions            if(target[j]!='B'|| target[(j+1)%length]!='A') {i=J;  Break; }        }        inttimes=0, Start,end; Long Longans=1;  for(intj=i;j<i+length;j++){            if(target[j%length]=='B'&&target[(j+1)%length]=='A'){                if(times==0) {Start= (j+length-1)%length;//record start position, remember-1} Times++; J++;//there's A + + in the loop.            }            Else {                if(times!=0){//if the number of BA groups has been recordedEnd=j%length;//Record End Position                    if(target[start]=='A'&&target[end]=='A') ans*=dp[times][0]; Else if(target[start]=='A'&&target[end]=='B') ans*=dp[times][1]; Else if(target[start]=='B'&&target[end]=='A') ans*=dp[times][2]; Else if(target[start]=='B'&&target[end]=='B') ans*=dp[times][3]; Times=0;//number of groups of records in a string clear 0                }            }        }        if(times!=0){//Don't forget the last group! Look at the way I implemented the construction, and the last group was ignored.end=i%length; if(target[start]=='A'&&target[end]=='A') ans*=dp[times][0]; Else if(target[start]=='A'&&target[end]=='B') ans*=dp[times][1]; Else if(target[start]=='B'&&target[end]=='A') ans*=dp[times][2]; Else if(target[start]=='B'&&target[end]=='B') ans*=dp[times][3]; } cout<<ans<<Endl; }    return 0;}
View Code

Not read the series ((((() ((

Algorithm Training Cowboys (DP)

Related Article

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.