1 / 6

数据结构题目讲解

数据结构题目讲解. 1. 字符串. 思路:从左往右看的话,每数到两个相同的就相除掉,然后把右边的字符拼接过来。 换句话说,一个一个字符的往栈里加,如果碰到相同的字符就消掉。. scanf("%s",s); int L=strlen(s); int top=0; for(int i=0;i<L;i++) { if(top && t[top-1]==s[i]) --top; else t[top++]=s[i]; } t[top]=0; puts(t);. 感冒病毒. 思路:用并查集将每一个社团的学生合并,最后 0 号学生所在集合大小即为答案.

hollis
Download Presentation

数据结构题目讲解

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数据结构题目讲解 1

  2. 字符串 思路:从左往右看的话,每数到两个相同的就相除掉,然后把右边的字符拼接过来。 换句话说,一个一个字符的往栈里加,如果碰到相同的字符就消掉。 scanf("%s",s); int L=strlen(s); int top=0; for(int i=0;i<L;i++) { if(top && t[top-1]==s[i]) --top; else t[top++]=s[i]; } t[top]=0; puts(t);

  3. 感冒病毒 思路:用并查集将每一个社团的学生合并,最后0号学生所在集合大小即为答案 scanf("%d%d",&n,&m); for(int j=0;j<n;j++)set[j]=j; for(int i=0;i<m;i++) { scanf("%d%d",&t,&a); for(int j=1;j<t;j++) { scanf("%d",&b); unionSet(a,b); } } int ret=0; for(int i=0;i<n;i++) if(find(i)==find(0))ret++; printf("%d\n",ret);

  4. 滑动的窗户 双端队列,保持队列里的数单调递减(增) void MA() { int top=0,tail=0; for(int i=0;i<n;i++) { if(q[top+1]+k<=i)top++; while(top!=tail&&a[q[tail]]<a[i])tail--; q[++tail]=i; if(i==n-1)printf("%d\n",a[q[top+1]]); else if(i>=k)printf("%d ",a[q[top+1]]); } }

  5. 弱点 思路:枚举每个位置作为j时,满足条件的i的个数和k的个数 memset(tree,0,sizeof(tree)); for(int i=0;i<N;i++) { scanf("%d",&a[i]); M=max(M,a[i]); } for(int i=0;i<N;i++) { c[i]=i-read(a[i]);//求出前面i-1个数中大于a[i]的个数 update(a[i]); } long long ans=0; memset(tree,0,sizeof(tree)); for(int i=N-1;i>=0;i--) { ans+=(long long)c[i]*(long long)read(a[i]); //read(a[i])表示从i+1到N-1中小于等于a[i]的个数 update(a[i]); } printf("%I64d\n",ans);

  6. Thank You! 6

More Related