Leetcode刷题笔记29:201703-1-2-3-4(全部评测一遍过)

第二十六天 2021-4-10 备战CSP
刷题模块:CSP 201703-1-2-3-4
最后一天复习CSP,明天正式考试。
之后将降低算法学习强度,每周学习一章AcWing教程即可。

一、201703-1

Easy.

#include<iostream>
#include<cstdio>
using namespace std;

int n,k,w;
int sumW,sumP;

int main(){
	sumP=0;sumW=0;
	
	scanf("%d %d",&n,&k);
	while(n--){
		scanf("%d",&w);
		sumW+=w;
		if(sumW>=k){
			sumP++;
			sumW=0;
		}
	}
	if(sumW) sumP++;
	
	printf("%d\n",sumP);
	return 0;
}

二、201703-2

Easy.
双向链表

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1010;

int l[N],r[N],h;

int n,m;
int stu,dis;

void init(){
	h=0;
	//node 0 is a head node, node n+1 is a tail node
	for(int i=0;i<=n+1;i++){
		r[i]=i+1;
		l[i]=i-1;
	}
}
void reStu(int u){
	if(u==0 || u==n+1) printf("Error\n");
	r[l[u]]=r[u];
	l[r[u]]=l[u];
}
void addStu(int b,int u){
	if(b==n+1) printf("Error\n");
	l[u]=b,r[u]=r[b];
	l[r[b]]=u,r[b]=u;
}
void moveStu(int stu,int dis){
	int b=stu;
	reStu(stu);
	if(dis>0){
		while(dis--){
			b=r[b];
		}
		addStu(b,stu);
	}
	else{
		while(dis++){
			b=l[b];
		}
		addStu(l[b],stu);
	}
}
int main(){
	scanf("%d %d",&n,&m);
	init();
	while(m--){
		scanf("%d %d",&stu,&dis);
		moveStu(stu,dis);
	}
	
	for(int i=r[h];i!=n+1;i=r[i]){
		printf("%d ",i);
	}
	puts("");
}

三、201703-3

算法是弱项,大模拟是强项,先做第三题
一遍过,爽

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void transLine(string& line);

bool isParg;
string bt[7]={"","<h1>","<h2>","<h3>","<h4>","<h5>","<h6>"};
string et[7]={"","</h1>","</h2>","</h3>","</h4>","</h5>","</h6>"};
string bl[2]={"<ul>","<li>"};
string el[2]={"</ul>","</li>"};
string p[2]={"<p>","</p>"};
string em[2]={"<em>","</em>"};
string a[3]={"<a href=\"","\">","</a>"};


void transBlock(string& l){
	//title processing
	if(l[0]=='#'){
		int i=1;
		while(l[i]=='#') {i++;}
		l.erase(0,i+1);
		
		transLine(l);
		
		l.insert(0,bt[i]);
		l.insert(l.size(),et[i]);
		
		cout<<l<<endl;
	}
	//list processing
	else if(l[0]=='*'){
		cout<<bl[0]<<endl;
		do{
			l.erase(0,2);
			
			transLine(l);
			
			l.insert(0,bl[1]);
			l.insert(l.size(),el[1]);
			
			cout<<l<<endl;
		}while(getline(cin,l) && l[0]=='*');
		cout<<el[0]<<endl;
	}
	//paragraph processing
	else{
		transLine(l);
		l.insert(0,p[0]);
		
		string nl;
		while(getline(cin,nl) && nl!=""){
			transLine(l);
			cout<<l<<endl;
			l=nl;
		}
		
		transLine(l);
		l.insert(l.size(),p[1]);
		cout<<l<<endl;
	}
}
void transLine(string& l){
	int i=0,j=0,k;
	//deal with em
	while(i<l.size()){
		while(l[i]!='_' && i<l.size()){i++;}
		if(i!=l.size()){
			l.erase(i,1);
			l.insert(i,em[j]);
			j=(j+1)%2;
			i++;
		}
	}
	//deal with link
	i=0;
	string text,link;
	while(i<l.size()){
		while(l[i]!='[' && i<l.size()){i++;}
		if(i!=l.size()){
			j=i+1;
			while(l[j]!=']'){j++;}
			text=l.substr(i+1,j-i-1);
			k=++j;
			while(l[k]!=')'){k++;}
			link=l.substr(j+1,k-j-1);
			l.erase(i,k-i+1);
			string temp=a[0]+link+a[1]+text+a[2];
			l.insert(i,temp);
		}
	}
}


int main(){
	freopen("in.txt","r",stdin);
	string line;
	while(getline(cin,line)){
		if(line=="") continue;
		transBlock(line);
	}
	return 0;
}

四、201703-4

评测一遍过,考试前一天,还是要稳住心态

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100010,M=N*2;

struct Edge{
	int a,b,c;
	Edge():a(0),b(0),c(0){}
	Edge(int a,int b,int c):a(a),b(b),c(c){}
	bool operator<(const Edge& e)const{
		return c<e.c;
	}
}e[M];
int n,m;
int idx,p[N];
bool st[N];

int find(int x){
	if(p[x]!=x) p[x]=find(p[x]);
	return p[x];
}

int Kruskal(){
	int i=0;
	int maxL;
	while(find(1)!=find(n)){
		int a=e[i].a;
		int b=e[i].b;
		int x=find(a),y=find(b);
		if(x==y){i++;continue;}
		else{
			p[x]=y;
			maxL=e[i].c;
			i++;
		}
	}
	
	return maxL;
}

int main(){
	idx=0;
	memset(st,0,sizeof st);
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		p[i]=i;
	}
	for(int i=0;i<m;i++){
		scanf("%d %d %d",&e[i].a,&e[i].b,&e[i].c);
	}
	sort(e,e+m);
	
	printf("%d\n",Kruskal());
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值