HDU 4295 4 substrings problem (State compression)

Source: Internet
Author: User

[Cpp]
/*
The memory is too large. I didn't want to solve this problem. Later I decided to pass the data.
As a result, the memory is too large and the status is compressed,
*/
 
# Include <cstdio>
# Include <cstring>
Constint INF = 4100;
Int d1 [4096] [16] [64], d2 [4100] [16] [64];
Char S [4100], a [4] [70];
Int can [4100] [4];
Int next [70];
 
Void getNext (char * s, int len)
{
Next [0] =-1;
Int I, j;
For (I = 0, j =-1; I <len; ++ I)
{
If (j =-1 | s [I] = s [j])
{
I ++;
J ++;
If (s [I] = s [j])
Next [I] = next [j];
Else
Next [I] = j;
}
Else
J = next [j];
}
}
 
Void match (int c, char * S, char * s, int len_S, int len_s)
{
Int I, j;
For (I = 0, j = 0; I <len_S ;)
{
If (j =-1 | S [I] = s [j])
{
I ++;
J ++;
If (j = len_s)
{
Can [I-len_s] [c] = 1;
I = I-len_s + 1;
J = 0;
}
}
Else
J = next [j];
}
}
 
Int min (int a, int B)
{
Return a <B? A: B;
}
 
Int max (int a, int B)
{
Return a> B? A: B;
}
 
Int main ()
{
// Freopen ("e: // data. in", "r", stdin );
Int up = (1 <4 );
While (scanf ("% s", S, a [0], a [1], a [2], a [3])! = EOF)
{
Int I, j, k, l, m, kk;
Int len_S, len_a [4];
Len_S = strlen (S );
For (I = 0; I <4; ++ I)
Len_a [I] = strlen (a [I]);
Memset (can, 0, sizeof (can ));
For (I = 0; I <4; ++ I)
{
GetNext (a [I], len_a [I]);
Match (I, S, a [I], len_S, len_a [I]);
}

Int _ max = 0, _ min = 4100;
Memset (d1,-1, sizeof (d1 ));
For (I = 0; I <4096; ++ I)
For (j = 0; j <16; ++ j)
For (k = 0; k <64; ++ k)
D1 [I] [j] [k] = INF;
Memset (d2,-1, sizeof (d2 ));
For (j = 0; j <4; ++ j)
{
If (can [0] [j])
D1 [0] [1 <j] [len_a [j] = d2 [0] [1 <j] [len_a [j] = len_a [j];
}
For (I = 1; I <len_S; ++ I)
{

For (j = 0; j <4; ++ j)
{
If (can [I] [j])
{
For (k = 0; k <up; ++ k)
{
If (k & (1 <j ))
{
Kk = k;
For (l = max (0, I-64); l <I; ++ l)
{
For (m = 0; m <= 64; ++ m)
{
If (d1 [l] [k] [m]! = INF & l + m> = I)
D1 [I] [k] [len_a [j] = min (d1 [I] [k] [len_a [j], d1 [l] [k] [m]);
If (d2 [l] [k] [m]! =-1 & l + m> = I)
D2 [I] [k] [len_a [j] = max (d2 [I] [k] [len_a [j], d2 [l] [k] [m]);
}
}
}
Else
{
Kk = (k | (1 <j ));
For (l = max (0, I-64); l <I; ++ l)
{
For (m = 0; m <= 64; ++ m)
{
If (d1 [l] [k] [m]! = INF & l + m> = I)
D1 [I] [kk] [len_a [j] = min (d1 [I] [kk] [len_a [j], d1 [l] [k] [m] + len_a [j]);
If (d2 [l] [k] [m]! =-1 & l + m> = I)
D2 [I] [kk] [len_a [j] = max (d2 [I] [kk] [len_a [j], d2 [l] [k] [m] + len_a [j]);
}
}
}
If (I + len_a [j]> = len_S)
{
_ Min = min (_ min, d1 [I] [kk] [len_a [j]);
_ Max = max (_ max, d2 [I] [kk] [len_a [j]);
}
}
}
}
}
Printf ("% d \ n", _ min, _ max );
}
Return 0;
}

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.