poj2774 Long Long Message

本文介绍了一道经典的字符串处理问题——寻找两个字符串间的最长公共子串,并详细解析了如何通过后缀数组的方法高效解决该问题。

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

poj2774 Long Long Message

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother.

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:

  1. All characters in messages are lowercase Latin letters, without punctuations and spaces.
  2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long.
  3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer.
    E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc.
  4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different.

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat.

Background:
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be.

Why ask you to write a program? There are four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :(

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

题目大意

给定两个字符串A和B,求最长公共子串。

题解

运用后缀数组,已知height数组定义为sa[i-1]与sa[i]的最长公共前缀,而一个串中任意两个后缀的最长公共前缀一定在height中取到。那么将两个字符串用分隔符连接起来,满足sa[i-1]与sa[i]分属两个串的最大的height值即为所求。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

const int N = 200000 + 10;
char s[N];
int a[N], p, q, n, df, k, ans;
int v[N], sa[2][N], rank[2][N], height[N];

void init(){
    scanf("%s", s+1);
    int len = strlen(s+1);
    for(int i = 1; i <= len; i++)
        a[i] = (int) s[i];
    df = len + 1;
    a[len+1] = (int) '$';
    scanf("%s", s+1);
    n = strlen(s+1) + len + 1;
    for(int i = len + 2; i <= n; i++)
        a[i] = (int) s[i-len-1];
}

void calsa(int *sa1, int *rank1, int *sa2, int *rank2){
    for(int i = 1; i <= n; i++) v[rank1[sa1[i]]] = i;
    for(int i = n; i >= 1; i--)
        if(sa1[i] > k)
            sa2[v[rank1[sa1[i]-k]]--] = sa1[i] - k;
    for(int i = n - k + 1; i <= n; i++)
        sa2[v[rank1[i]]--] = i;
    for(int i = 1; i <= n; i++)
        rank2[sa2[i]] = rank2[sa2[i-1]] +
        (rank1[sa2[i-1]] != rank1[sa2[i]] || rank1[sa2[i-1]+k] != rank1[sa2[i]+k]);
}
void calheight(){
    int k = 0;
    for(int i = 1; i <= n; i++){
        if(k) k--;
        int j = sa[p][rank[p][i]-1];
        while(a[i+k] == a[j+k]) k++;
        height[rank[p][i]] = k;
    }
}
void work(){
    p = 0, q = 1;
    for(int i = 1; i <= n; i++) v[a[i]]++;
    for(int i = 1; i <= 256; i++) v[i] += v[i-1];
    for(int i = 1; i <= n; i++) sa[p][v[a[i]]--] = i;
    for(int i = 1; i <= n; i++)
        rank[p][sa[p][i]] = rank[p][sa[p][i-1]] + (a[sa[p][i-1]] != a[sa[p][i]]);
    k = 1;
    while(k < n){
        calsa(sa[p], rank[p], sa[q], rank[q]);
        p ^= 1, q ^= 1, k <<= 1;
    }
    calheight();
    for(int i = 2; i <= n; i++){
        if(height[i] > ans)
            if(sa[p][i-1] < df && df < sa[p][i] || sa[p][i] < df && df < sa[p][i-1])
                ans = height[i];
    }
    printf("%d\n", ans);
}

int main(){
    init();
    work();
    return 0;
}
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值