1032 Sharing-------21分

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;

char word[100005];
int node[100005];

int main()
{
    int firadd1,firadd2,n;
    cin>>firadd1>>firadd2>>n;
    int add,next;
    char c;
    map<char,int> kkk;
    for(int i=0;i<n;i++)
    {
        cin>>add>>c>>next;
        node[add]=next;
        word[add]=c;
        kkk[c]=add;
    }

    vector<char> word1,word2;
    int start=firadd1;
    while(start!=-1)
    {
        word1.push_back(word[start]);
        start=node[start];
    }
        start=firadd2;
    while(start!=-1)
    {
        word2.push_back(word[start]);
        start=node[start];
    }

    int same=0;
    int end1=word1.size()-1;
    int end2=word2.size()-1;
    while(word1[end1]==word2[end2])
    {
        same++;
        end1--;
        end2--;
        if(end1<0||end2<0)
            break;
    }
    if(same==0)
        cout<<"-1";
    else
        printf("%05d",kkk[word1[end1+1]]);  //错误1:答案要补0
    return 0;
}

还有段错误 也搞不清为什么。。。 
不过说真的,静态链表的写法真的惊到我了,原来这题可以写这么简单。菜是原罪。

#include<iostream>

using namespace std;

struct {
	bool used = false;
	int next = -2;
}num[100000];

int main()
{
	int h1, h2, n, i, temp1, temp2;
	char c;
	cin >> h1 >> h2 >> n;
	for (i = 0; i < n; i++)
	{
		cin >> temp1 >> c >> temp2;
		num[temp1].next = temp2;
	}
	while (h1 != -1)
	{
		num[h1].used = true;
		h1 = num[h1].next;
	}
	while (h2 != -1)
	{
		if (num[h2].used)
		{
			printf("%05d", h2);
			return 0;
		}
		h2 = num[h2].next;
	}
	if (h2 == -1)
		cout << "-1";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值