国庆普及模拟赛-3

题目链接

file:///C:/Users/Administrator/Desktop/1003/%E4%B8%8B%E5%8F%91%E6%96%87%E4%BB%B61003/20241003.pdf

题解:file:///C:/Users/Administrator/Desktop/%E9%A2%98%E8%A7%A3.pdf

总结:本次考试反映出在字符串上面有很大问题,要转向复习,练题

T1:按照题目要求进行模拟,不用考虑复杂度

错因:在调试数字为两位数时,经常出错,所以选择只考虑数字为一位数的情况,最终30分

AC代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int T,n,cnt,pos;
string a,b,c;
int main() {
    freopen("letter.in","r",stdin);
    freopen("letter.out","w",stdout);
	cin >> T;
	while(T--) {
		cin >> n >> a;
		a=" "+a,b="";
		for(int i=1;i<=n;++i) {
			if(a[i]>='a'&&a[i]<='z')b+=a[i];//直接在字符串后面补上当前字符
			else {
				cnt=0,pos=i;
				while(a[pos]>='0'&&a[pos]<='9') {//计算要复读多少次
					cnt*=10;
					cnt+=a[pos]-'0';
					pos++;
				}
				i=pos-1;
				c=b,cnt--;
				while(cnt--)b+=c;//复读
			}
		}
		cout<<b<<endl;
	}
	return 0;
}

T2:

思路:直接暴力模拟pos在哪,O( n^2)的算法绝对会爆,n<10^6

所以考虑使用前缀合(见代码)

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
long long p1[N],p0[N];
signed main() {
	freopen("soldier.in","r",stdin);
	freopen("soldier.out","w",stdout);
	long long  n;
	string s;
	cin>>n>>s;
	s =" "+ s;
	for(int i=1; i<s.size(); i++) {
		p1[i]=p1[i-1]+(s[i]=='1')*i;
		p0[i]=p0[i-1]+(s[i]=='0')*i;
	}
	long long ans = 1e9;
	for(int i=1; i<=n; i++) {
		ans=min(ans,abs(p0[i-1]-p1[n]+p1[i-1]));
	}
	cout<<ans<<endl;
}

T3:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int t,tot;
string a[N];
struct node{
	int x,y;
}b[N];
bool operator <(node x,node y){
	return x.y<y.y;
}
long long read() {
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9') {
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9') {
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}

int main() {
	freopen("lie.in","r",stdin);
	freopen("lie.out","w",stdout);
	cin>>t;
	while(t--) {
		int n=read();
		for(int i=1; i<=n; i++)cin>>a[i];
		string s;
		cin>>s;
		int len=s.size();
		for(int i=0; i<len; i++) {
			for(int j=1; j<=n; j++) {
				int cnta=-1,cntb=i-1;
				int lenx=a[j].size();
				while(cnta<lenx-1&&cntb<len-1&&a[j][cnta+1]==s[cntb+1]) cnta++,cntb++;
				if(cnta==lenx-1) {
					b[++tot].x=i,b[tot].y=cntb;
				}
			}
		}
		sort(b+1,b+tot+1);
		int tmp=-1;
		for(int i=1; i<=tot; i++) {
			if(b[i].x<=tmp&&b[i].y>=tmp) continue;
			s[b[i].y]='*';
			tmp=b[i].y;
		}

		cout<<s<<endl;
		tot=0;
	}
	return 0;
}

T4:

cpp 复制代码
#include<bits/stdc++.h>

using namespace std;
const int Mod=1e9+7,N=1e5+5;
int n,m,q,a[N],dep[N],b[N][2];
vector<int> v[N],vec;

void dfs(int x,int fa) {
	dep[x]=dep[fa]+1;
	for(int i=0; i<v[x].size(); i++) {
		int y=v[x][i];
		if(y==fa) continue;
		dfs(y,x);
	}
	return;
}
bool cmp(int x,int y) {
	return dep[x]<dep[y];
}
int main() {
	freopen("kodori.in","r",stdin);
	freopen("kodori.out","w",stdout);
	cin>>n>>m>>q;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
		vec.push_back(i);
	}
	for(int i=1; i<n; i++) {
		int x,y;
		cin>>x>>y;
		v[x].push_back(y),v[y].push_back(x);
	}
	dfs(1,0);
	for(int i=1; i<=m; i++) {
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for(int i=1; i<=q; i++) {
		int t,u,k;
		cin>>t>>u>>k;
		b[u][t-1]=(b[u][t-1]+k)%Mod;
	}
	sort(vec.begin(),vec.end(),cmp);
	for(int i=0; i<n; i++) {
		int x=vec[i];
		for(int j=0; j<v[x].size(); j++) {
			int y=v[x][j];
			if(dep[y]>dep[x]) b[y][1]=(b[y][1]+b[x][1])%Mod;
		}
	}
	for(int i=n-1; i>=0; i--) {
		int x=vec[i];
		for(int j=0; j<v[x].size(); j++) {
			int y=v[x][j];
			if(dep[y]<dep[x]) b[y][0]=(b[y][0]+b[x][0])%Mod;
		}
	}
	for(int i=1; i<=n; i++) {
		int ans=((a[i]+b[i][1])%Mod+b[i][0])%Mod;
		cout<<ans<<" ";
	}
	return 0;
}
相关推荐
沐籽李22 分钟前
HuDiff在抗体人源化项目中的工程化落地
人工智能·算法·aidd·抗体设计
依然范特东24 分钟前
强化学习笔记6--IS、PPO、TD、DQN、Actor-Critic
笔记·算法
一次旅行2 小时前
RLHF全链路深度解析:Reward Model数学推导+PPO完整实战,对比GRPO轻量化方案
人工智能·算法·机器学习
Wang's Blog2 小时前
AI Agent白手起家29: Few Shot 提示词工程实战
人工智能·算法
June`3 小时前
warp shuffle指令
c++·人工智能·算法·cuda
geovindu3 小时前
go:Bit Operation Algorithm
开发语言·后端·算法·golang·位运算法
坏柠3 小时前
C语言进阶:从零实现一个可靠的环形缓冲区,吃透回绕、边界与并发
java·c语言·算法
乱七八糟的屋子3 小时前
【C++数值计算】NumCpp超详细入门教程(纯正Numpy语法、零学习成本、C++首选科学计算库、性能压测对比)
c++·数值计算·numpy·高性能计算·科学计算·ndarray
choumin3 小时前
C程序能调用extern “C“声明的带默认参数的C++函数吗?
c语言·c++·extern c·默认参数
AmyLin_20014 小时前
PDF 色彩保真工程实践【3】上手实践:工程结构、依赖集成与一键构建运行
c++·visualstudio·pdf·zlib·vcpkg·libtiff·cmyk