HDU-2594 Simpsons 'den den Talents

Source: Internet
Author: User
Tags first string

Problem descriptionhomer: Marge, I just figured out a way to discover some of the talents we weren't aware we had.
MARGE: Yeah, what is it?
HOMER: Take me for example. I want to find out if I have a talent in politics, OK?
MARGE: OK.
HOMER: So I take some politician's name, say Clinton, and try to find the length of the longest prefix
In Clinton's name that is a Suffix in my name. That's how close I am to being a politician like Clinton
MARGE: Why on earth choose the longest prefix that is a suffix ???
HOMER: Well, our talents are deeply hidden within ourselves, Marge.
MARGE: So how close are you?
HOMER: 0!
MARGE: I'm not surprised.
HOMER: But you know, you must have some real math talent hidden deep in you.
MARGE: How come?
HOMER: Riann and Marjorie gives 3 !!!
MARGE: Who the heck is Riann?
HOMER: Never mind.
Write a program that, when given strings S1 and S2, finds the longest prefix of S1 that is a suffix of S2.
Inputinput consists of two lines. The first line contains S1 and the second line contains S2. you may assume all letters are in lowercase.
Outputoutput consists of a single line that contains the longest string that is a prefix of S1 and a suffix of S2, followed by the length of that prefix. if the longest such string is the empty string, then the output shocould be 0.
The lengths of S1 and S2 will be at most 50000.
Sample Input
clintonhomerriemannmarjorie
 
Sample output
0rie 3 Question: Find the longest Public String Of the prefix of the first string and the suffix of the second string. The idea is to connect the two strings, use the next [] array of KMP to obtain the string prefix and suffix matching. Finally, let's talk about the length of the answer string.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn = 1000005;int next[maxn], n, m;char pattern[maxn];char s1[maxn], s2[maxn];void getNext() {int len = strlen(pattern);next[0] = next[1] = 0;for (int i = 1; i < len; i++) {int j = next[i];while (j && pattern[i] != pattern[j])j = next[j];next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;}int ans = next[len];if (ans == 0)printf("0\n");else {s1[ans] = '\0';if (n > m) {if (ans > m)printf("%s %d\n", s2, m);else printf("%s %d\n", s1, ans);}else {if (ans > n)printf("%s %d\n", s1, n);else printf("%s %d\n", s1, ans);}}}int main() {while (scanf("%s%s", s1, s2) != EOF) {n = strlen(s1), m = strlen(s2);memcpy(pattern, s1, sizeof(s1));strcat(pattern, s2);getNext();}return 0;}


HDU-2594 Simpsons 'den den Talents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.