0
点赞
收藏
分享

微信扫一扫

第5期:自训 2022/1/18

践行数据分析 2022-01-20 阅读 29
c语言

​​​​​​​1 UVA514 铁轨

#include<bits/stdc++.h>
using namespace std;
const int MAX_N=1000+10;
int train[MAX_N];
int main(){
	int n,A,B,ok;
	while(scanf("%d",&n),n){
		stack<int> s;
		while(1){
			scanf("%d",&train[1]);
			if(train[1]==0) break;
			for(int i=2;i<=n;i++){
				scanf("%d",&train[i]);
			}
			A=B=ok=1;
			while(B<=n)  //如果驶向B的车个数等于n,则循环结束。   
        	{  
            	//如果驶向C的车等于驶向B的车的序列号,直接该将车驶进B
            	//可以得出若满足如果驶向C的车等于驶向B的车的序列号,A车一定直接驶入B
            	if(A==train[B]) { A++,B++;}   
            	//否则,则判断栈顶的(即在C最上面)车是否等于驶向B的车
            	//若成立,则将车驶入B
            	else if(!s.empty()&&s.top()==train[B]) {s.pop(),B++;}  
            	//将车驶进C   
            	else if(A<=n) s.push(A++);  
            	//如果车全部都驶入C,循环还没有结束,意味着所给的train顺序不能实现   
            	else  
            	{ 	 
                	ok=0;  
                	break;  
            	}  
        	}  
        	printf("%s\n",ok?"Yes":"No");
		}  
        printf("\n");
    }
	return 0;
} 

2 UVA442 矩阵链乘 Matrix Chain Multiplication

#include<bits/stdc++.h>
using namespace std;
struct Matrix{
	int a,b;
	Matrix(int a=0,int b=0):a(a),b(b){}
}m[26];
stack<Matrix> s;
int main(){
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		string name;
		cin>>name;
		int k=name[0]-'A';
		cin>>m[k].a>>m[k].b; 
	}
	string expr;
	while(cin>>expr){
		int len=expr.length();
		bool error=false;
		int ans=0;
		for(int i=0;i<len;i++){
			if(isalpha(expr[i])) s.push(m[expr[i]-'A']);
			else if(expr[i]==')'){
				Matrix m2=s.top(); s.pop();
				Matrix m1=s.top(); s.pop();
				if(m1.b!=m2.a){error=true; break;}
				ans+=m1.a*m1.b*m2.b;
				s.push(Matrix(m1.a,m2.b)); 
			}
		}
		if(error) printf("error\n");
		else printf("%d\n",ans);
	}
	return 0;
}

3 UVA11988 破损的键盘 Broken Keyboard (a.k.a. Beiju Text)

//非AC码,仅提供注释
#include<bits/stdc++.h>  
using namespace std;  
char s[100005];  
int next[100005];//next存的值为当前光标的位置后接的下一个值的位置;  
int main()  
{  
    int cur,last;  
    while(~scanf("%s",s+1))//从1,2,3,4,。。。n开始存。  
    {  
        cur=last=0;  
        next[0]=0;//存储链表第一个元素的位置  
        for(int i=1;s[i]!='\0';i++)  
        {  
            char ch=s[i];  
            if(ch=='[') cur=0;//光标移动到最前面  
            else if(ch==']') cur=last;//next[last]=0;把这个等价看作next[0],类似于从头开始存,,  
            else  
            {  
                next[i]=next[cur];//输入的值连往next光标位置存的值;  
                next[cur]=i;//当前光标连接往后一个输入的值  
                if(last==cur) last=i;  
                cur=i;//光标变到后一个值得位置  
            }  
        }  
        for(int i=next[0];i!=0;i=next[i])  
        {  
            printf("%c",s[i]);  
        }  
        printf("\n");  
    }  
    return 0;  
}  

AC码

#include <bits/stdc++.h>
using namespace std;
char s[100005];
int last,now,nxt[100005];
int main(){
    while(~scanf("%s",s+1)){
        int n=strlen(s+1);
        last=0;
        now=0;
        nxt[0]=0;
        for(int i=1;i<=n;i++){
            char ch=s[i];
            if(ch=='[')now=0;
            else if(ch==']')now=last;
            else{
                nxt[i]=nxt[now];
                nxt[now]=i;
                if(now==last)last=i;
                now=i;
            }
        }
        for(int i=nxt[0];i!=0;i=nxt[i])printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}

4 UVA12657 移动盒子 Boxes in a Line

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
int left[maxn],right[maxn],inv;
void link(int L,int R)
{
	left[R]=L;
	right[L]=R;
} 
long long number(int n)
{
	int x=0;
	long long total=0;
	for(int i=1;i<=n;i++)
	{
		x=right[x];
		if(i%2!=0)total+=x;
	}
	if(inv!=0&&n%2==0)total=(long long)n*(n+1)/2-total;
	return total;
} 
int main()
{
	int n,m,T=1;
	while(scanf("%d %d",&n,&m)==2)
	{
		for(int i=1;i<=n;i++)
		{
			left[i]=i-1;
			right[i]=(i+1)%(n+1);
		}
		right[0]=1;
		left[0]=n;
		inv=0;
		while(m--)
		{
			int order,X,Y;
			scanf("%d",&order);
			if(order==4)
			{
				inv=!inv;
				continue;
			}
			scanf("%d %d",&X,&Y);
			if(order==3&&right[Y]==X)swap(X,Y);//X,Y相邻要特殊考虑
			if(order!=3&&inv)order=3-order;
			if(order==1&&left[Y]==X)continue;
			if(order==2&&right[Y]==X)continue;
			int LX,RX,LY,RY;
			LX=left[X];
			RX=right[X];
			LY=left[Y];
			RY=right[Y];
			if(order==1)
			{
				link(X,Y);
				link(LX,RX);
				link(LY,X);
			}
			else
			if(order==2)
			{
				link(Y,X);
				link(LX,RX);
				link(X,RY);
			}
			else
			if(order==3)
			{
				if(right[X]==Y)
				{
					link(LX,Y);
					link(Y,X);
					link(X,RY); 
				}
				else
				{
					link(LX,Y);
					link(Y,RX);
					link(LY,X);
					link(X,RY);
				}
			}
		}
		printf("Case %d: %lld\n",T++,number(n));
	}
	return 0;
}
举报

相关推荐

0 条评论