【poj2774】Long Long Message 后缀数组
来源:互联网 发布:像素软件招聘 编辑:程序博客网 时间:2024/06/05 04:43
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
yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother
Sample Output
27
Source
POJ Monthly–2006.03.26,Zeyuan Zhu,”Dedicate to my great beloved mother.”
学不动后缀自动机和后缀树的我来搞后缀数组了QAQ
虽然敲的倍增,但实在不会写双关键字排序的计数排序QAQ看网上的代码好蛋疼
连起来然后取lcp最大值就行了,由于用的sort所以复杂度
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int SZ = 1000010; int n,lcp[SZ],l,sa[SZ],rank[SZ],k = 1,tmp[SZ];bool cmp_sa(int i,int j){ if(rank[i] != rank[j]) return rank[i] < rank[j]; else { int x = i + k <= l ? rank[i + k] : -1; int y = j + k <= l ? rank[j + k] : -1; return x < y; }}void getsa(char s[]){ l = strlen(s); for(int i = 0;i <= l;i ++) { sa[i] = i; rank[i] = i == l ? -1 : s[i]; } for(k = 1;k <= l;k <<= 1) { sort(sa,sa + 1 + l,cmp_sa); tmp[sa[0]] = 0; for(int i = 1;i <= l;i ++) tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0); for(int i = 0;i <= l;i ++) rank[i] = tmp[i]; } int h = 0; for(int i = 0;i <= l;i ++) rank[sa[i]] = i; lcp[0] = 0; for(int i = 0;i < l;i ++) { int j = sa[rank[i] - 1]; if(h) h --; while(i + h < l && j + h < l) { if(s[i + h] == s[j + h]) h ++; else break; } lcp[rank[i] - 1] = h; }}int ask(){ int ans = 0; for(int i = 0;i < l;i ++) { // if((sa[i] < n && sa[i + 1] > n) || (sa[i] > n && sa[i + 1] < n)) if((sa[i] < n) != (sa[i + 1] < n)) ans = max(ans,lcp[i]); } return ans;}char s[SZ],s1[SZ];int main(){ scanf("%s",s); s[n = strlen(s)] = '$'; scanf("%s",s1); int m = strlen(s1); for(int i = 0;i < m;i ++) s[i + n + 1] = s1[i];// printf("%s\n",s); getsa(s);// for(int i = 1;i <= l;i ++)// printf("%d ",sa[i]); puts(""); printf("%d\n",ask()); return 0;}/*yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother*/
- POJ2774《Long Long Message》方法:后缀数组
- POJ2774:Long Long Message(后缀数组)
- 【后缀数组】【poj2774】【 Long Long Message】
- 后缀数组 - poj2774 Long Long Message
- 后缀数组 poj2774 Long Long Message
- 【POJ2774】Long Long Message【后缀数组】
- poj2774 Long Long Message 后缀数组
- 【poj2774】Long Long Message 后缀数组
- poj2774 Long Long Message(后缀数组)
- [POJ2774]Long Long Message(后缀数组)
- [后缀数组]poj2774 Long Long Message
- [POJ2774]Long Long Message(后缀数组)
- poj2774 Long Long Message(后缀数组)
- POJ2774 Long Long Message(后缀数组)
- POJ2774 Long Long Message 【后缀数组lcp】
- poj2774——Long Long Message(后缀数组)
- 【后缀数组】【最长公共子串】Long Long Message POJ2774
- [POJ2774][后缀数组求LCP]Long Long Message
- 【转】LinkedList、ArrayList各自的使用场景分析
- *** Collection <__NSArrayM: 0x7f9fb3c48bc0> was mutated while being enumerated问题
- Android开发中遇到的一些小问题
- (二)ActiveMQ 点对点系统模型
- 线程间 COM实例的传递
- 【poj2774】Long Long Message 后缀数组
- 有关禅道的学习记录
- 模块化与解耦
- (三)ActiveMQ 发布订阅系统模型
- Eclipse 升级ADT的问题
- 事件传播的三个阶段
- 【CSS3】关于box-shadow的扩展半径
- 探究问题:ssh+extjs 权限角色用户功能的实现
- boost::bind的使用方法