Question: unappeared substrings
Description
[Note] the subdigit string in this question does not necessarily appear consecutively in the parent digit string. For example, we define 1 3 as string 1 5 3.
But 3 5 is not a substring of 1 5 3.
All substrings of string 1, 5, and 3 are:
1
5
3
1 5
5 3
1 3
1 5 3
A total of 7.
[Question description] There is a number string with the length of n, where numbers 1, 2, 3,..., q (5 <= q <= 9). SubRaY encounters a question
The question is, you need to find a string with the minimum length (the number that appears is also 1 .. q) so that the string is not a substring of the numeric string.
To simplify the problem, you only need to output the length of this string.
For example
1 3 5 2 4 1 3 2 2 2 3 4 1 5 3 2 (q = 5)
The number substrings with the length of 1 and 2 have all appeared, but you cannot find the substring S '= 4 4 4. Therefore, the answer is 3.
[Data range]
For 30% of the data, 1 <= n <= 20, q = 5
For 100% of the data, 1 <= n <= 100000,5 <= q <= 9
Input Format
Number of the first row, string length n, and number of numbers displayed q
The next n rows indicate the number of each digit in the number string.
Output Format
Minimum length of a child string that does not appear
Sample Input
18 5
1
3
5
2
4
1
3
5
2
2
2
2
3
4
1
5
3
2
Sample output
3
Note: There are two solutions for this question. The first is DP: We first define the definition of matching: a valid substring. For each number, if each number from 1-q can be successfully matched with a length of not less than k, then the successful matching length of this number is k + 1, so we have the state transition equation: f [I] = min {f [k]} + 1; (I, [1 .. q], k> I ). Because we are looking for the matching length behind it, we need to push it backwards. The second method is the set idea in mathematics. We divide the number sequence into k parts, ensure that each copy contains 1 .. q can be divided into a maximum of k portions, so the final answer is k + 1.
Code 1:
var a:array[0..100000] of longint; f:array[0..9] of longint; i,j,k,ans,n,q:longint;function min(x,y:longint):longint;begin if x<y then exit(x); exit(y);end;function max(x,y:longint):longint;begin if x>y then exit(x); exit(y);end;begin readln(n,q); for i:=1 to n do readln(a[i]); ans:=0; for i:=n downto 1 do begin k:=maxlongint; for j:=1 to q do k:=min(k,f[j]); f[a[i]]:=k+1; end; for i:=1 to q do ans:=max(ans,f[i]); writeln(ans);end.
Code 2:
var i,j,k,ans,n,m,q:longint; vv:boolean; v:array[0..10] of boolean;begin readln(n,q); ans:=1; fillchar(v,sizeof(v),false); for i:=1 to n do begin vv:=true; for j:=1 to q do if not v[j] then begin vv:=false; break; end; if vv then begin fillchar(v,sizeof(v),false); inc(ans); end; readln(k); v[k]:=true; end; writeln(ans);end.