记录PTA甲级以及C++部分语法知识1032*

我自己写的有点小问题,先贴上自己的,日后找找问题。

底下一版参考柳婼大神:https://blog.csdn.net/liuchuo/article/details/52144527?utm_source=blogxgwz9

#include<iostream>
#include<vector>

using namespace std;

struct letter{
	char c;
	int next;
}L[100000];

struct element{
	char c;
	int index;
};
vector<element> w1,w2;

int main(){
	int ad1,ad2,N,i,ad,j;
	cin>>ad1>>ad2>>N;
	for(i=0;i<N;i++){
		cin>>ad;
		cin>>L[ad].c>>L[ad].next;
	}
	//cout<<ad1<<"******"<<ad2<<endl;
	while(ad1!=-1){
		element e;
		e.c=L[ad1].c;
		e.index=ad1;
		w1.push_back(e);
		ad1=L[ad1].next;
	}
	while(ad2!=-1){
		element e;
		e.c=L[ad2].c;
		e.index=ad2;
		w2.push_back(e);
		ad2=L[ad2].next;
	}
	int cnt=0;
	i=w1.size()-1;
	j=w2.size()-1;
	//cout<<"i:"<<i<<"j:"<<j<<endl;
	

	for(;w1[i].c==w2[j].c &&i>=0&&j>=0;i--,j--)
		cnt++;

	if(cnt==0)
		cout<<"-1";
	else
		printf("%05d",w1[i+1].index);
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;

struct letter{
	char c;
	int next;
	bool flag;
}L[100000];

int main(){
	int ad1,ad2,N,i,ad;
	cin>>ad1>>ad2>>N;
	for(i=0;i<N;i++){
		cin>>ad;
		cin>>L[ad].c>>L[ad].next;
		L[ad].flag=false;
	}
	//cout<<ad1<<"******"<<ad2<<endl;
	while(ad1!=-1){
		L[ad1].flag=true;
		ad1=L[ad1].next;
	}
	while(ad2!=-1){
		if(L[ad2].flag==true){
			printf("%05d",ad2);
			return 0;
		}
		ad2=L[ad2].next;
	}	
	printf("-1");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值