牛客小白月赛19

B.「木」迷雾森林 (记忆化搜索、DP)

题目链接

#include<bits/stdc++.h>

#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=3005;
const int mod=2333;

int mapp[maxn][maxn];
bool book[maxn][maxn];
int dp[maxn][maxn];
int m,n;
int nextz[2][2]={0,1,-1,0};
int sx,sy,ex,ey;

template<class T>inline void read(T &res)
{
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}

bool check(int x,int y)
{
	if(x<0||y<0||x>=m||y>=n)
		return false;
	return true;
}
int dfs(int x,int y)
{
	if(x==ex&&y==ey)
		return dp[x][y]=1;
	if(dp[x][y]!=-1)
		return dp[x][y];
	int sum=0;
	for(int i=0;i<2;i++)
	{
		int tx=x+nextz[i][0];
		int ty=y+nextz[i][1];
		if(!check(tx,ty)||mapp[tx][ty]==1||book[tx][ty])
			continue;
		book[tx][ty]=true;
		sum+=dfs(tx,ty);
		sum%=mod;
		book[tx][ty]=false;
	}
	return dp[x][y]=sum;
}
int main()
{
	read(m);
	read(n);
	for(int i=0;i<m;i++)
	{
		for(int j=0;j<n;j++)
		{
			read(mapp[i][j]);
		}
	} 
	sx=m-1;sy=0;
	ex=0;ey=n-1;
	for(int i=0;i<maxn;i++)
		for(int j=0;j<maxn;j++)
			dp[i][j]=-1;
	memset(book,false,sizeof(book));
	book[sx][sy]=true;
	int ans=dfs(sx,sy);
	printf("%d\n",ans%mod);
	return 0;
} 

C.「土」秘法地震(矩阵前缀和)

题目链接

#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int maxn=1005;
int n,m,k;
int nextz[4][2]={1,1,1,-1,-1,-1,-1,1};
int pre[maxn][maxn];
string smap[maxn];

bool check(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
		return false;
	return true;
}
int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>smap[i];
		smap[i]=" "+smap[i];	
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			pre[i][j]=pre[i][j-1]+pre[i-1][j]-pre[i-1][j-1];
			if(smap[i][j]=='1')
				pre[i][j]++;
		}
	}
	int ans=0;
	for(int i=1;i<=n-k+1;i++)
	{
		for(int j=1;j<=m-k+1;j++)
		{
			int ex,ey;
			ex=i+k-1;
			ey=j+k-1;
			if(pre[ex][ey]-pre[ex][j-1]-pre[i-1][ey]+pre[i-1][j-1]>0)
				ans++;
		}
	}
	cout << ans << endl;
	return 0;
} 

D.「金」初心如金(异或、思维)

题目链接

#include<bits/stdc++.h>

#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=1000005;
int n;
LL a[maxn];
template<class T>inline void read(T &res)
{
    char c;
    T flag=1;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
    res=c-'0';
    while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';
    res*=flag;
}
int main()
{
	//ios::sync_with_stdio(false);
	read(n);
	for(int i=0;i<n;i++)
		read(a[i]);
	int ans=0;
	for(int i=0;i<n-1;i++)
	{
		if(a[i+1]%2==0)
			puts("1");
		else
			puts("0");
	}
	return 0;
}

F.「水」悠悠碧波(字符串、KMP)

题目链接

#include<bits/stdc++.h>

#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=100005;
string s;
int nextz[maxn];
void get_next(string t)
{
	nextz[0]=-1;
	int i,j;
	i=0;
	j=-1;
	int len=t.length();
	while(i<len)
	{
		if(j==-1||t[i]==t[j])
		{
			i++;j++;
			nextz[i]=j;
		}
		else
			j=nextz[j];
	}
}
bool KMP(string s,string t)
{
	int i,j;
	i=0;j=0;
	int len1=s.length();
	int len2=t.length();
	while(i<len1&&j<len2)
	{
		if(j==-1||s[i]==t[j])
		{
			i++;
			j++;
		}
		else
			j=nextz[j];
		if(j>=len2)
		{
			return true;
		}
			
	}
	return false;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>s;
//	for(int i=0;i<=s.length();i++)
//		cout << nextz[i] << ' ';
//	cout << endl;
	string ans;
	for(int i=1;3*i<=s.length();i++)
	{
		string pre,last;
		pre=s.substr(0,i);
		last=s.substr(s.length()-i);
		if(pre!=last)
			continue;
		string zz=s.substr(i,s.length()-2*i);
		get_next(pre);
		if(KMP(zz,pre))
			ans=pre;
	}
	cout << ans << endl; 
	return 0;
}

G.「金」点石成金(DFS、枚举暴力)

题目链接

#include<bits/stdc++.h>

#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=20;
int n;
int a[maxn],b[maxn],c[maxn],d[maxn];
LL maxi;
void dfs(LL money,LL power,int step)
{
	if(money<0)
		money=0;
	if(power<0)
		power=0;
	if(step==n)
	{
		maxi=max(maxi,money*power);
		return ;	
	}
	dfs(money+a[step],power-b[step],step+1);
	dfs(money-d[step],power+c[step],step+1);
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i]>>b[i]>>c[i]>>d[i];
	}
	maxi=0;
	dfs(0,0,0);
	cout << maxi << endl;
	return 0;	
} 
posted @ 2019-12-01 21:01  loveyixuan  阅读(247)  评论(0)    收藏  举报