BestCoder #37 Rikka with wood sticks DFS暴力

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 92    Accepted Submission(s): 23


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:


Yuta have a wood stick of length  n  which consists of  n  linked sticks of length  1 . So it has  n1  connection points. Yuta finds that some sticks of length  1  of the wood stick are not strong. So he wants to choose three different connection points to cut it into four wood sticks and only one of them contains wood sticks which are not strong. And Yuta wants to minimize the length of this piece which contains bad wood sticks. Besides, Rikka wants to use the other three wood sticks to make a triangle. Now she wants to count the number of the ways to cut the wood sticks which can make both Yuta and herself happy.


It is too difficult for Rikka. Can you help her?
 

Input
This problem has multi test cases (no more than  20 ). For each test case, The first line contains two numbers  n,m(1n1000000,1m1000) . The next line contains m numbers (some of them may be same) – the position of each wood sticks which is not strong.
 

Output
For each test cases print only one number – the ways to cut the wood sticks.
 

Sample Input
   
   
   
   
6 1 3 5 1 3
 

Sample Output
   
   
   
   
2 0
 

Source
BestCoder Round #37 ($)


开始的时候这道题目想麻烦了,主要是思路没有理清,边想边写,效率低还容易错。下面分析一下思路。


(1)找出字符串中所有的问号的个数count,并且标记它们的位置pos[]。

(2)当count==0的时候,直接判断是否是回文数,输出

(3)当count==1的时候,如果'?'正好在中间的时候,直接把这个问号赋值为'a',然后按照第一种情况处理

(4)剩下的所有情况,一定可以组成一个非回文数,这时候,我们可以使用比较暴力的方式,就是直接枚举每一个问号填入的字符,但有一个小技巧,就是任何一个问号处只可能是a或者b,这样情况就会少很多。


#include<iostream>
using namespace std;
char str[1001];
int pos[1001];
int len,count;
bool flag;
bool judge();
void dfs(int p)
{
    if(flag==false)
    {
        return;
    }
    if(p==count)
    {
        if(judge())
        {
            return;
        }
        else
        {
            cout<<str<<endl;
            flag=false;
            return;
        }
    }

    for(char a='a';a<='b';a++)
    {
        str[pos[p]]=a;
        dfs(p+1);
    }
}

bool judge()
{
    int i,j;
    for(i=0,j=len-1;i<len/2;i++,j--)
    {
        if(str[i]!=str[j])
        {
            return false;
        }
    }
    return true;
}



int main()
{
    int i,j;
    while(cin>>len>>str)
    {
        count=0;
        flag=true;
        for(i=0;i<len;i++)
        {
            if(str[i]=='?')
                pos[count++]=i;
        }
        if(count==0)
        {
            if(judge())
            {
                cout<<"QwQ"<<endl;
            }
            else
            {
                cout<<str<<endl;
            }
        }
        else if(count==1&&str[len/2]=='?')
        {
            if(judge())
            {
                cout<<"QwQ"<<endl;
            }
            else
            {
                str[len/2]='a';
                cout<<str<<endl;
            }
        }
        else
        {
            dfs(0);
        }

    }
}




你可能感兴趣的:(with,DFS,暴力,Wood,BestCoder,Rikka,Stic,#37)