ACM-ACMICPC (AC, dynamic planning/traversing all strings, three pieces of code)

Source: Internet
Author: User
Document directory
  • Description:
  • Input:
  • Output:
  • Sample Input:
  • Sample Output:

ACMICPC
Time Limit: 1000 MS Memory Limit: 32768 K

Description:

The uppercase letter A-Z corresponds to an integer [-13, 12], so a string corresponds to an integer column. The sum of the integer columns corresponding to the string is called the characteristic value of the string. For example, if the integer column {-13,-11,-1} corresponding to the string ACM is set to (-13) + (-11) + (-1) =-25; the feature value of its sub-string AC is-24; the feature value of its sub-string M is-1. For a string, find the maximum feature value of all the substrings of the string.

Input:

The positive integer N (1 <= N <= 1000) in the first line indicates that there are N groups of test data, each of which is composed of a string. A string can only consist of uppercase letters and A-Z and cannot exceed 1000 characters in length.

Output:

There are N rows of output, and each row is the output result of a group of test data.

Sample Input:
2ACMANGRY
Sample Output:
-115

Http://acm.zjut.edu.cn/ShowProblem.aspx? ShowID = 1021

 

/* ------------------------------------------------------------------------------ Non-dynamic planning solution: Practice: traverse all strings to find the largest one. Time: 297MS memory: 196 K -------------------------------------------------------------------------------- */# include <stdio. h> char szStr [1004]; int a [1004]; int main (void) {int I = 0; int j = 0; int t = 0; int n = 0; int nMax =-1000; scanf ("% d", & t); getchar (); while (t --> 0) {gets (szStr); nMax =-1000; j = 1; I = 0; while (szStr [I]! = '\ 0') {a [j ++] = szStr [I]-65-13; I ++;} n = j-1; a [0] = 0; for (I = 1; I <= n; ++ I) {if (a [I]> = nMax) {nMax = a [I];} a [I] + = a [I-1]; if (a [I]> = nMax) {nMax = a [I];} for (j = 1; j <I; ++ j) {if (a [I]-a [j]> = nMax) {nMax = a [I]-a [j] ;}} printf ("% d \ n", nMax) ;}return 0 ;}

 

/* ------------------------------------- Dynamic planning solution: Time: 9 ms memory: 200 K ----------------------------------------- */# include <stdio. h ># define max (x, y) (x)> (y )? (X): (y) char szStr [1004]; int a [1004]; int B [1004]; int main (void) {int I = 0; int j = 0; int t = 0; int n = 0; int nMax =-1000; scanf ("% d", & t); getchar (); while (t --> 0) {gets (szStr); nMax =-1000; j = 1; I = 0; while (szStr [I]! = '\ 0') {a [j ++] = szStr [I]-65-13; I ++;} n = j-1; B [0] =-1000; nMax =-1000; for (I = 1; I <= n; ++ I) {B [I] = max (a [I], B [I-1] + a [I]); if (nMax <B [I]) {nMax = B [I] ;}} printf ("% d \ n", nMax);} return 0 ;}

 

/* Shard dynamic planning solution: However, the time and space complexity are optimized. In terms of time, it is only a traversal. In terms of space, strings are not used for saving because it is input and processed. Time: 7 ms memory: 196 K running */# include <stdio. h> # define max (x, y) (x)> (y )? (X): (y) int B [1004]; int main (void) {int I = 0; int j = 0; int t = 0; int n = 0; int nMax =-1000; int nTemp = 0; char chCur = '\ 0'; scanf ("% d", & t); getchar (); while (t --> 0) {nMax =-1000; B [0] =-1000; I = 1; while (chCur = getchar ())! = '\ N') {nTemp = chCur-65-13; B [I] = max (nTemp, B [I-1] + nTemp ); if (nMax <B [I]) {nMax = B [I];} I ++;} printf ("% d \ n", nMax);} 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.