关于读取字符。。。
查看原帖
关于读取字符。。。
284715
遮云壑楼主2021/9/5 11:43

有没有哪位大佬给我讲讲这cin和getchar有什么区别,好几次了,本地AC提交WA,把getchar改成cin就行了??

#include<bits/stdc++.h>
#define N 105
using namespace std;
inline void read(int& x)
{
	x=0;int y=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')y=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x=x*y;
}

int n,m,a[N],dp[(1<<10)][(1<<10)][3],sum[(1<<10)];

inline int getsum(int x)
{
	int ans=0;
	while(x)
	{
		if(x&1)ans++;
		x>>=1;
	}
	return ans;
}

int main(){
	read(n),read(m);
	int pow=(1<<m);char ch;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>ch;//没错没错就是这里
			a[i]<<=1;
			if(ch=='H')a[i]++;
		}
	}
	for(int i=0;i<pow;i++)sum[i]=getsum(i);
	for(int s=0;s<pow;s++)
	{
		if((s&a[0])||(s&(s<<1))||(s&(s<<2)))continue;
		dp[0][s][0]=sum[s];
	}
	for(int L=0;L<pow;L++)
	{
		for(int S=0;S<pow;S++)
		{
			if(L&S||L&a[0]||S&a[1]||(L&(L<<1))||(L&(L<<2))||(S&(S<<1))||(S&(S<<2)))continue;
			dp[L][S][1]=sum[L]+sum[S];
		}
	}
	for(int i=2;i<n;i++)
	{
		for(int L=0;L<pow;L++)
		{
			if((L&(L<<1))||(L&(L<<2)||L&a[i-1]))continue;
			for(int S=0;S<pow;S++)
			{
				if(S&a[i]||S&(S<<1)||S&(S<<2)||L&S)continue;
				for(int LL=0;LL<pow;LL++)
				{
					if(LL&a[i-2]||LL&L||LL&S||LL&(LL<<1)||LL&(LL<<2))continue;
					dp[L][S][i%3]=max(dp[L][S][i%3],dp[LL][L][(i-1)%3]+sum[S]);
				}
			}
		}
	}
	int ans=0;
	for(int L=0;L<pow;L++)
	{
		for(int S=0;S<pow;S++)
		{
			ans=max(ans,dp[L][S][(n-1)%3]);
		}
	}
	printf("%d\n",ans);
	return 0;
}

然后下面的是90分的,样例WA了hhhhh

#include<bits/stdc++.h>
#define N 105
using namespace std;
inline void read(int& x)
{
	x=0;int y=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')y=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x=x*y;
}

int n,m,a[N],dp[(1<<10)][(1<<10)][3],sum[(1<<10)];

inline int getsum(int x)
{
	int ans=0;
	while(x)
	{
		if(x&1)ans++;
		x>>=1;
	}
	return ans;
}

int main(){
	read(n),read(m);
	int pow=(1<<m);char ch;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			ch=getchar();//没错没错就是这里
			a[i]<<=1;
			if(ch=='H')a[i]++;
		}
		ch=getchar();//把回车读掉
	}
	for(int i=0;i<pow;i++)sum[i]=getsum(i);
	for(int s=0;s<pow;s++)
	{
		if((s&a[0])||(s&(s<<1))||(s&(s<<2)))continue;
		dp[0][s][0]=sum[s];
	}
	for(int L=0;L<pow;L++)
	{
		for(int S=0;S<pow;S++)
		{
			if(L&S||L&a[0]||S&a[1]||(L&(L<<1))||(L&(L<<2))||(S&(S<<1))||(S&(S<<2)))continue;
			dp[L][S][1]=sum[L]+sum[S];
		}
	}
	for(int i=2;i<n;i++)
	{
		for(int L=0;L<pow;L++)
		{
			if((L&(L<<1))||(L&(L<<2)||L&a[i-1]))continue;
			for(int S=0;S<pow;S++)
			{
				if(S&a[i]||S&(S<<1)||S&(S<<2)||L&S)continue;
				for(int LL=0;LL<pow;LL++)
				{
					if(LL&a[i-2]||LL&L||LL&S||LL&(LL<<1)||LL&(LL<<2))continue;
					dp[L][S][i%3]=max(dp[L][S][i%3],dp[LL][L][(i-1)%3]+sum[S]);
				}
			}
		}
	}
	int ans=0;
	for(int L=0;L<pow;L++)
	{
		for(int S=0;S<pow;S++)
		{
			ans=max(ans,dp[L][S][(n-1)%3]);
		}
	}
	printf("%d\n",ans);
	return 0;
}
2021/9/5 11:43
加载中...