国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

poj2774 Long Long Message

2019-11-08 01:55:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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:

All characters in messages are lowercase Latin letters, without punctuations and spaces. 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. 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. 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

題目大意

給定兩個(gè)字符串A和B,求最長(zhǎng)公共子串。

題解

運(yùn)用后綴數(shù)組,已知height數(shù)組定義為sa[i-1]與sa[i]的最長(zhǎng)公共前綴,而一個(gè)串中任意兩個(gè)后綴的最長(zhǎng)公共前綴一定在height中取到。那么將兩個(gè)字符串用分隔符連接起來(lái),滿足sa[i-1]與sa[i]分屬兩個(gè)串的最大的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;}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青州市| 天峨县| 义马市| 崇义县| 南阳市| 宁德市| 焉耆| 个旧市| 荣昌县| 双峰县| 霍林郭勒市| 和顺县| 晋州市| 上饶市| 宣威市| 沿河| 昌黎县| 桐乡市| 青龙| 新蔡县| 舒城县| 鄂托克前旗| 茶陵县| 南皮县| 新余市| 平和县| 富蕴县| 蒙阴县| 盘锦市| 湘潭县| 新野县| 武义县| 榆树市| 河北区| 宁化县| 石泉县| 孝义市| 浏阳市| 澄迈县| 千阳县| 孟村|