哈希值索引:恢复古诗

题目:
jxtxzzw和好朋友去探险,在山洞里jxtxzzw发现了许多古诗,他很兴奋地呼唤同伴过来,用相机把这些古诗拍下来。每个人都选了一部分进行拍照。

回到学校以后,所有人把相机中的照片导出,保存在电脑。

这时jxtxzzw发现,竟然当时没有让伙伴们按照顺序拍照。

现在这些照片根本就不是有序的,所以这些古代诗歌的碎片完全连不成一个整体。

好在现在有一个线索,在相邻的两张照片中,一定有一句是重复的。分别出现在两张碎片的头和尾。

在这里插入图片描述
样例:
input
2 3
This is the 3rd line.
This is the 4th line.
This is the 5th line.
This is the 1st line.
This is the 2nd line.
This is the 3rd line.

output
This is the 1st line.
This is the 2nd line.
This is the 3rd line.
This is the 4th line.
This is the 5th line.
思路:
将每一段的第一句和最后一句建立索引,最后一句和第一句相同的可以找到对应的句子。

代码:

#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
string s[1111][111];
map<string, int> parahead, paratail;
int p, l;
int main() {
    cin >> p >> l;
    cin.ignore();
    for (int i = 0; i < p; i++)
        for (int j = 0; j < l; j++) {
            getline(cin, s[i][j]);
            if (!j)
                parahead[s[i][j]] = i;
            if (j == l - 1)
                paratail[s[i][j]] = i;
        }
    int first = 0;
    for (int i = 0; i < p; i++) {
        if (!paratail.count(s[i][0])) {
            first = i;
            break;
        }
    }
    int point = first;
    for (int i = 0; i < p; i++) {
        if (point == first)
            cout << s[point][0] << endl;
        for (int j = 1; j < l; j++) {
            cout << s[point][j] << endl;
        }
        point = parahead[s[point][l - 1]];
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值