The maximum number of pairs that XTU 1151 bus does not contain each other DP

Source: Internet
Author: User

BusAccepted: 49 Submit: 478 Time Limit: 1000 MS Memory Limit, one of them is the bus. Xiaoqiang's home has many bus stops at school. Each bus station has an English name. Xiaoqiang prefers to take a bus, but he has a strange requirement that the English names of the bus's upper and lower stations must be the same with the first letter and get on and off at the same station, otherwise, Xiaoqiang would rather walk through the station to take a taxi or even go directly to school. The English name of each bus station between Xiaoqiang's home and school is provided. Can Xiaoqiang take a bus at most if he does not go back? Enter multiple groups of samples. The first behavior of each group is a non-negative integer n (n <= 1000), indicating that there are n bus stops between Xiaoqiang's home and school. In the next n rows, each line has an English name and each line contains no more than 100 characters. Output the maximum number of rides for each group of samples. Example Input Samples OUTPUT 12 SourceXTUCPC2013 [cpp]/* Train of Thought refer to my shoes * Subject: Xiaoqiang cannot take a bus on the same platform (each platform is separated by its first letter) get off the bus and get on the bus on the platform. The first letter of the platform must be the same as that of the platform on the bus. * He can walk through all the platforms. How many times can Xiaoqiang make a car ?? * Solution: Use dp [I] [2] (I <= n) to solve the problem. I indicates each platform. dp [I] [0] indicates walking to this platform, dp [I] [1] indicates that you are on this platform by car. * state transition equation: dp [j] [0] = max (dp [I] [1], dp [I] [0], dp [j] [0]) (0 <= I <j ), dp [j] [1] = max (dp [I] [0] + 1, dp [j] [1]) (0 <= I <j ); */# include <stdio. h> # include <string. h> int a [2000]; char s [2000]; int dp [2000] [2]; int mmax (int a, int B) {return a> B? A: B;} int main () {int n, I, j; while (scanf ("% d", & n )! = EOF) {for (I = 0; I <n; I ++) {scanf ("% s", s ); a [I] = s [0]-'0';} memset (dp, 0, sizeof (dp); for (I = 0; I <n; I ++) for (j = 0; j <I; j ++) {if (a [I] = a [j]) dp [I] [1] = mmax (dp [I] [1], dp [j] [0] + 1 ); // else cannot be added. Because you can skip this location, dp [I] [0] is also required. Otherwise, you only need to get the value of the bus. // {www.2cto.com dp [i] [0] = mmax (dp [I] [0], dp [j] [0]); dp [I] [0] = mmax (dp [I] [0], dp [j] [1]); //} int ans = 0; for (I = 0; I <n; I ++) {ans = mmax (ans, dp [I] [0]); ans = mmax (ans, dp [I] [1]);} printf ("% d \ n", ans);} 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.