国庆普及模拟赛-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;
}
相关推荐
小师兄吃牛肉37 分钟前
C语言中的nullptr有必要吗
c语言·c++·算法
WL学习笔记1 小时前
日期类(C++)
开发语言·c++·日期类实现
H.莓飛2 小时前
【C++】命名空间、缺省参数、函数重载与引用
linux·开发语言·c++·visual studio
一只小小的芙厨2 小时前
【线性DP】
学习·算法·动态规划
xxwl5852 小时前
数据结构知识点和代码实现总结(C语言实现)
c语言·开发语言·数据结构
奋发向前wcx3 小时前
CSP-J复赛模拟赛5 王晨旭补题 2026.10.5
算法
汉克老师3 小时前
GESP2026年9月认证C++八级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程
罗湖老棍子4 小时前
C Looooops(信息学奥赛一本通- P1640)
算法·数论·同余·裴蜀定理·扩展欧几里得
郝学胜_神的一滴5 小时前
C++ Templates 06:搞懂模板代码的三种组织方式
c++·visual studio
C++ 老炮儿的技术栈5 小时前
sizeof操作符
c语言·c++·人工智能·mfc·c