Zoj 3675 pressure DP

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4918

Yesterday's ranking, I thought I was thinking, and my teammates told me to press DP and gave up directly. After the game, I checked my teams' code and searched the internet, the Code of teammates is actually the shortest. Worship ~~~~~~~

The idea is teammate a.l.

DP [s] = min (DP [s], DP [s '] + 1)

Among them, s' can be achieved through one-time nail cutting or reverse nail cutting.

As for the inner layer loop, 0-M is because ---- From the last bit of the Clipper, the last bit is moved from 0 to m, which is enough for all the situations.


#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <iostream>#include <iomanip>#include <cmath>#include <map>#include <set>#include <queue>using namespace std;#define ls(rt) rt*2#define rs(rt) rt*2+1#define ll long long#define ull unsigned long long#define rep(i,s,e) for(int i=s;i<e;i++)#define repe(i,s,e) for(int i=s;i<=e;i++)#define CL(a,b) memset(a,b,sizeof(a))#define IN(s) freopen(s,"r",stdin)#define OUT(s) freopen(s,"w",stdout)const ll ll_INF = ((ull)(-1))>>1;const double EPS = 1e-8;const int MAXN = 22;const int INF =0x3f3f3f3f3f3f3f3f;int dp[1<<MAXN];int main(){    //IN("zoj3675.txt");    int up,down;    int n,m;    char str[MAXN];    while(~scanf("%d",&n))    {        scanf("%s%d",str,&m);        int len=strlen(str);        up=down=0;        for(int i=0;i<len;i++)        {            if(str[i]=='*')            {                up+=(1<<i);                down+=(1<<(n-i-1));            }        }        CL(dp,INF);        dp[(1<<m)-1]=0;        for(int i=(1<<m)-1;i>=0;i--)            for(int j=0;j<m;j++)            {                dp[i&~(up<<j)]=min(dp[i&~(up<<j)],dp[i]+1);                dp[i&~(down<<j)]=min(dp[i&~(down<<j)],dp[i]+1);            }        printf("%d\n",dp[0]>=INF?-1:dp[0]);    }    return 0;}


Zoj 3675 pressure DP

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.