t1扫雷
二维数组,方向数组
#include<cstdio> using namespace std; char c[105][105];int a[105][105],n,m; int dx[]={0,1,1,1,0,-1,-1,-1},dy[]={1,1,0,-1,-1,-1,0,1}; int main(){ freopen("game.in","r",stdin); freopen("game.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ scanf("%s",c[i]+1); for(int j=1;j<=m;++j) if(c[i][j]=='#') for(int k=0;k<8;++k) ++a[i+dx[k]][j+dy[k]]; } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j) c[i][j]=='#'?printf("#"):printf("%d",a[i][j]); printf("\n"); } return 0; }
t2 music
枚举每一段旋律,即单词,对应枚举歌曲的每一个位置,判断是否一一对应相同,相同则累加分数。
#include<cstdio> #include<cstring> using namespace std; char c[15][105],s[105];int n,w[15],l,ans; bool check(int x,int len,int wz){ for(int i=0;i<len;++i) if(c[x][i]!=s[i+wz]) return 0; return 1; } int main(){ // freopen("music.in","r",stdin); // freopen("music.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%s%d",c[i],&w[i]); scanf("%s",s); l=strlen(s); for(int i=1;i<=n;++i){ int l1=strlen(c[i]); for(int j=0;j<=l-l1;++j) if(check(i,l1,j)) ans+=w[i]; } printf("%d",ans); return 0; }
t3 sum 最大子段和
//这是一道很好的最大子段和的题目,正着一遍,倒着一遍 #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #define int long long using namespace std; int n,f[100005],g[100005],a[100005],ans = -0x7fffffff,mxf[100005],mxg[100005]; signed main(){ scanf("%lld",&n); for(int i = 1; i <= n; i++){ scanf("%lld",&a[i]); } //以当前位置结尾的最大子段和,同时记录在它之前的最大子段和的值 for(int i = 1; i <= n; i++){ if(f[i-1] < 0) f[i] = a[i]; else f[i] = f[i-1] + a[i]; ans = max(ans, f[i]); mxf[i] = ans; // cout<<mxf[i]<<" "; } //倒着来一遍 ans = -0x7fffffff; for(int i = n; i >= 1; i--){ if(g[i+1] < 0) g[i] = a[i]; else g[i] = g[i+1] + a[i]; ans = max(ans, g[i]); mxg[i] = ans; } //枚举分界点 for(int i = 1; i <= n; i++) if(mxf[i] + mxg[i+1] > ans) ans = mxf[i]+mxg[i+1]; printf("%lld\n",ans); return 0; } /* 7 2 -4 3 -1 2 -4 3 */