qbxt 模拟题 2

博客主要涉及算法相关内容,提到一种没有算法的算法,其错点是a->b可多次出现,还有当25个元素一一对应后第26个自然对应这一细节。此外,还涉及最长上升子序列问题的nlogn序以及模拟栈、缩点或搜索等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

no.1迷

            算法:没有算法的算法。

            错点:a->b可以出现多次

            细节:当有25 个已经一 一对应之后,第26个自然对应。

            代码:

           

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
char a[1500],b[1500],c[1500],to[1000];
int fl[1000];
int main(){
	freopen("enc.in","r",stdin);
	freopen("enc.out","w",stdout);
	scanf("%s",a); scanf("%s",b);scanf("%s",c);
	int l=strlen(a)-1;
	for(int i=0;i<=l;i++){
		if(fl[b[i]-'0']){
			if(a[i]!=to[b[i]-'0']){
				printf("ERROR");
			    return 0;
			}
		}
		else fl[b[i]-'0']=1,to[b[i]-'0']=a[i];
	}	
	int lc=strlen(c)-1;
	for(int i=0;i<=lc;i++){
		if(fl[c[i]-'0']!=1){
			printf("ERROR");
			return 0;
		}
	}
	for(int i=0;i<=lc;i++){
		printf("%c",to[c[i]-'0']);
	}
	fclose(stdin);fclose(stdout);
	return 0;
} 

no.2

   最长上升子序列问题nlogn序

  模拟栈:

   

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100007;
int INF=10000007;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
int n,a[N],top;
int search(int l,int r,int x){
	int wz=0;
	while(l<=r){
		int mid=(l+r)/2;
		if(a[mid]>x){
			wz=mid;r=mid-1;
		}
		else l=mid+1;
	}
	return wz;
}
int main(){
	freopen("sort.in","r",stdin);
	freopen("sort.out","w",stdout);
	n=read();
	a[0]=-INF;
	for(int i=1;i<=n;i++){
	  	int x=read();
	  	if(x>a[top]){
	  		top++;
	  		a[top]=x;
	  	}
	  	else{
	  		a[search(1,top,x)]=x;
	  	}
	}
	printf("%d",top);
	fclose(stdin);fclose(stdout);
	return 0;
}

no.3戏

 缩点或搜索

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5007;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
char b[N];
int a[N][N],n,num[N],ans[5],fl[N][N],anss[5],flag=0;
void dfs(int x,int step){
	if(flag==1) return;
	ans[step]=x;
	if(step==3){
		if(fl[ans[3]][ans[1]]==1){
            flag=1;
			anss[1]=ans[1];
			anss[2]=ans[2];
			anss[3]=ans[3];
		}
		return;
	}
	for(int i=1;i<=num[x];i++){
		dfs(a[x][i],step+1);
	}
}
int main(){
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	n=read();
    for(int i=1;i<=n;i++){
    	scanf("%s",b);
    	for(int j=1;j<=n;j++){
    		fl[i][j]=b[j-1]-'0';
    		if(b[j-1]-'0'==1){
    			num[i]++;
    			a[i][num[i]]=j;
    		}
    	}
    }
    for(int i=1;i<=n;i++){
    	if(flag==0)
	      dfs(i,1);
       if(flag==1) break;
    }
    if(flag==0) printf("-1");
    else printf("%d %d %d",anss[1],anss[2],anss[3]);
    fclose(stdin);fclose(stdout);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值