先总结下:本来能轻松搞6道,剩下的也都有点思路,结果却不是像自己想的这样。代码能力确实有点弱,思路不清晰的题只有两道,其他的思路还是比较清晰的,实现时细节问题非常重要,细节决定成败。继续努力,一直相信:毅力更重要,不能不学习,一天学一点点,用心去做,用心去学,收获或许来的比较晚,但是一定会有的。努力吧,蒟蒻。
A.读了半天没读懂题,nlogn的板子搞上去挂了5发,读了3遍题,猜出了一点东西,蒙对了,就是找这个序列中不一样的数的个数。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
int a[100005];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
map<int,int> m;
scanf("%d",&n);
a[0]=1;
int num,sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
if(!m[num])
{
m[num]++;
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
B.水题,直接计算就可以了。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
char a[200005];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",a);
int len=strlen(a),sum=0;
for(int i=0;i<len;i++)
{
sum+=(a[i]-'A'+1);
}
printf("%d\n",sum);
}
return 0;
}
C题:暴力求回文数的题目,下面的算法仅供参考,时间复杂度感觉还可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int a[10];
for( a[1]=1; a[1]<=9; a[1]++)
for( a[8]=0; a[8]<=9; a[8]++)
{
if(a[1]==a[8])
{
for(a[2]=0;a[2]<=9;a[2]++)
{
for( a[7]=0;a[7]<=9;a[7]++)
{
if(a[2]==a[7])
{
for( a[3]=0;a[3]<=9;a[3]++)
{
for(a[6]=0;a[6]<=9;a[6]++)
{
if(a[3]==a[6])
{
for(a[4]=0;a[4]<=9;a[4]++)
{
for(a[5]=0;a[5]<=9;a[5]++)
{
if(a[4]==a[5])
{
printf("%d%d%d%d%d%d%d%d\n",a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]);
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
D题:并查集+01背包。母鸡比赛时自己怎么了,然后一直卡,现在给个AC的代码。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
int pre[10005];
int fin(int x)
{
if(x==pre[x])
{
return x;
}
else
{
return pre[x]=fin(pre[x]);
}
}
int a[10005],b[10005];
void join(int x,int y)
{
int t1=fin(x);
int t2=fin(y);
if(t1!=t2)
{
pre[t1]=t2;
}
}
int dp[10005];
int main()
{
int n,m,c,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&c);
for(int i=2;i<=n;i++)
{
scanf("%d%d",&a[i],&b[i]);
}
for(int i=1;i<=n;i++)
{
pre[i]=i;
}
int u,v;
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
join(u,v);
}
int pos=fin(1);
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
if(pos==fin(i))
{
for(int j=500;j>=a[i];j--)
{
dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
}
}
}
printf("%d\n",dp[c]);
}
return 0;
}
F题:直接暴力,然后处理下极端数据就好了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[100050];
int main()
{
int t,sum;
scanf("%d",&t);
while(t--)
{
scanf("%s",str+1);
int len=strlen(str+1);
int a,b,c,d;
sum=0;
for(int i=1,j; i<=len; i++)
{
a=0;
b=0;
c=0;
d=0;
for(j=i; j<=len; j++)
{
if (str[j]=='L') a++;
if (str[j]=='O') b++;
if (str[j]=='V') c++;
if (str[j]=='E') d++;
if (a&&b&&c&&d)
{
break;
}
}
if(j==len+1&&!(a&&b&&c&&d))
{
break;
}
if(j<=len&&a&&b&&c&&d)
sum=sum+len-j+1;
}
printf("%d\n",sum);
}
return 0;
}
G题:一道爆搜的题目,有个细节需要注意一下:就是除了出口,其他点假如人和火同时到达是不能接着走的。
【AC代码】
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
char a[50][50];
int book1[50][50];
int book2[50][50];
int num[50][50];
int n,m;
int dir1[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
int dir2[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct node
{
int x,y,step;
};
void bfs1()
{
node fr,ne;
queue<node>q;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='*')
{
fr.x=i,fr.y=j;
fr.step=0;
q.push(fr);
}
}
}
num[fr.x][fr.y]=0;
while(!q.empty())
{
fr=q.front();
q.pop();
for(int i=0;i<8;i++)
{
ne.x=fr.x+dir1[i][0];
ne.y=fr.y+dir1[i][1];
if(ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m&&book1[ne.x][ne.y]==0)
{
book1[ne.x][ne.y]=1;
ne.step=fr.step+1;
num[ne.x][ne.y]=ne.step;
q.push(ne);
}
}
}
}
int bfs2(int sx,int sy,int ex,int ey)
{
node fr,ne;
fr.x=sx,fr.y=sy;
fr.step=0;
queue<node>q;
q.push(fr);
book2[fr.x][fr.y]=1;
while(!q.empty())
{
fr=q.front();
q.pop();
if(fr.x==ex&&fr.y==ey)
{
return fr.step;
}
for(int i=0;i<4;i++)
{
ne.x=fr.x+dir2[i][0];
ne.y=fr.y+dir2[i][1];
ne.step=fr.step+1;
if(ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m&&book2[ne.x][ne.y]==0)
{
book2[ne.x][ne.y]=1;
if(a[ne.x][ne.y]=='.')
{
if(ne.step>=num[ne.x][ne.y])
{
continue;
}
else
{
book2[ne.x][ne.y]=1;
q.push(ne);
}
}
else if(a[ne.x][ne.y]=='E')
{
if(ne.step<=num[ne.x][ne.y])
q.push(ne);
}
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int sx,sy,ex,ey;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
memset(book1,0,sizeof(book1)); memset(book2,0,sizeof(book2));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='#')
{
book2[i][j]=1;
}
if(a[i][j]=='S')
{
sx=i,sy=j;
}
else if(a[i][j]=='E')
{
ex=i,ey=j;
}
}
}
bfs1();
int re=bfs2(sx,sy,ex,ey);
if(re==-1)
{
printf("T_T\n");
}
else
{
printf("%d\n",re);
}
}
return 0;
}
H题:矩阵快速幂求斐波那契数列的第n项,当时带的模板就是这个题的,只是因为前面卡那两题卡的太严重,板子最后都没敲上。
【AC代码】
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
struct Mat
{
LL a[2][2];
void init()
{
memset(a,0,sizeof(a));
a[0][0]=1;
a[1][1]=1;
}
};
Mat mul(Mat a,Mat b)
{
Mat ans;
ans.init();
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
ans.a[i][j]=0;
for(int k=0;k<2;k++)
{
ans.a[i][j]+=a.a[i][k]*b.a[k][j];
}
ans.a[i][j]%=1000000007;
}
}
return ans;
}
Mat power(Mat a,LL num)
{
Mat ans;
ans.init();
while(num)
{
if(num&1)
{
ans=mul(ans,a);
}
num/=2;
a=mul(a,a);
}
return ans;
}
int main()
{
long long n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(n==-1)
{
break;
}
if(n==0)
{
printf("0\n");
continue;
}
Mat a,ans;
a.a[0][0]=1,a.a[0][1]=1,a.a[1][0]=1,a.a[1][1]=0;
ans=power(a,n+1);
printf("%lld\n",ans.a[1][0]%1000000007);
}
}