Ultraviolet A 12385

Source: Internet
Author: User

---------------------------------------------------

Description:

I haven't understood this question many times. English is not enough.

It is required to find the non-conflicting "interesting" subsequence.

--------------------------------------------------

Answer:

Use dp. Mark the previous position where s [I] appears. If s [I] is not displayed, it is recorded as 0.

Dp [I] = max (dp [I-1], dp [last [s [I] + 1); last [s [I] is updated at the same time.

This method is correct: because the question is a non-conflicting string, this method will ignore the various strings between I and last [s [I. If a certain number between I and last [s [I] is selected as the starting point, the string ending with s [I] is obviously ignored. In addition, if there are 123121 overlapping strings, we will obviously choose to separate them, so we can only update the s [I] We encountered at the last update.

--------------------------------------------------

Source code:


[Cpp]
# Include <stdio. h>
 
# Define max (a, B) (a)> (B )? (A) (B ))
 
Int main ()
{
Int t = 0, n = 0;
Int s [100010];
Int I = 0;
Int dp [100010], last [100010];
Int ans = 0;
 
Scanf ("% d", & t );
While (t --)> 0)
{
Scanf ("% d", & n );
 
Ans = 0;
For (I = 1; I <= n; I ++)
{
Scanf ("% d", & s [I]);
Last [s [I] = 0;
 
}
 
Last [s [1] = 1;
Dp [1] = 0;
For (I = 2; I <= n; I ++)
{
If (last [s [I]! = 0)
Dp [I] = max (dp [I-1], dp [last [s [I] + 1 );
Else
Dp [I] = dp [I-1];
Last [s [I] = I;
Ans = max (dp [I], ans );
}
Printf ("% d \ n", ans );
}
 
Return 0;
}


Author: violet_xrym

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.