POJ 1226 (longest public substrings with Reverse Order)

Source: Internet
Author: User

Substrings
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 9639 Accepted: 3319
Description
Find the longest 'positive/inverse 'substring of some strings so that it is the substring of all strings (even in reverse order ).
Input
Number of first behavior data t, (1 <=t <= 10 ),
For each group of data, the number of first behavior strings is n (1 <= n <= 100), and the next n behavior strings (length cannot exceed 100 ).
Output
Each row has one number, indicating the length of the longest 'positive/inverse 'substring.
Sample Input
2
3
ABCD
BCDFF
BRCD
2
Rose
Orchid
Sample Output
2
2
Source
Tehran 2002 Preliminary
Or KMP, first enumerate the string in the first string, and then check whether it is a 'positive/inverse 'substring of other strings.

[Delphi]
Program P1226;
Const
Maxn = 100;
Maxt = 10;
Var
Tt, n, m, I, j, k, ans: longint;
Flag: boolean;
A: array [1 .. maxn] of string;
P: string;
Next: array [1 .. maxn] of longint;
 
Function kmp (a, B: string): boolean;
Var
I, j, n, m: longint;
Begin
I: = 1; j: = 0; next [1]: = 0;
N: = length (a); m: = length (B );
While (I <m) do
Begin
If (j = 0) or (B [I] = B [j]) then
Begin
Inc (I); inc (j );
If (B [I] <> B [j]) then next [I]: = j else next [I]: = next [j];
End else j: = next [j];
End;
 
I: = 0; j: = 0;
While (I <= n) and (j <= m) do
Begin
If (j = 0) or (a [I] = B [j]) then
Begin
Inc (I); inc (j );
End else j: = next [j];
End;
If (j> m) then exit (true );
Exit (false );
End;
Function ob_s (a: string): string;
Var
I, j, n: longint;
Begin
 
Ob_s: = ''; n: = length ();
For I: = n downto 1 do ob_s: = ob_s + a [I];
End;
Function compare (a, B: string): boolean;
Var
N, m: longint;
Begin
N: = length (a); m: = length (B );
If (n <> m) then exit (n <m );
For I: = 1 to n do
If a [I] <> B [I] then exit (a [I] <B [I]);
Exit (false );
End;
 
 
Begin
Readln (tt );
While (tt> 0) do
Begin
Ans: = 0;
Readln (n );
For I: = 1 to n do readln (a [I]);
 
For I: = 1 to length (a [1]) do
For j: = I to length (a [1]) do
Begin
P: = copy (a [1], I, j-I + 1 );
Flag: = true;
For k: = 2 to n do
Begin
If not (kmp (a [k], p) or kmp (a [k], ob_s (p) then
Begin
Flag: = false; break;
End; www.2cto.com
End;
If flag and (length (p)> ans) then ans: = length (p );
 
 
End;
 
Writeln (ans );
Dec (tt );
End;
End.


 

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.