ACM-ACMICPC (AC,動態規劃/遍曆所有字串,三份代碼)

來源:互聯網
上載者:User
文章目錄
  • Description:
  • Input:
  • Output:
  • Sample Input:
  • Sample Output:

ACMICPC
Time Limit:1000MS  Memory Limit:32768K

Description:

大寫字母A-Z分別對應整數[-13,12],因此,一個字串對應了一個整數列。我們把字串對應的整數列的和稱為該字串的特性值。例如:字串ACM對應的整數列為{-13,-11,-1},則ACM的特性值為(-13)+(-11)+(-1)=-25;其子串AC的特性值為-24;子串M的特性值為-1。給你一個字串,請找出該字串的所有子串的最大特性值。

Input:

第一行的正整數 N(1<=N<=1000)表示其後有N組測試資料,每組測試資料都由一個字串組成。字串只能由大寫字母A-Z組成,且字串的長度不會超過1000。

Output:

輸出有N行,每行是一組測試資料的輸出結果。

Sample Input:
2ACMANGRY
Sample Output:
-115

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

 

/*--------------------------------------------------------------------------------非動態規劃解法:做法:遍曆所有字串,找出最大那個。時間:297MS記憶體:196K--------------------------------------------------------------------------------*/#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 ;}

 

/*---------------------------------------動態規劃解法:時間:9MS記憶體:200K---------------------------------------*/#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 ;}

 

/*--------------------------------------------------------------------------------------動態規劃解法:不過,最佳化了時間和空間複雜度。在時間方面邊輸入邊處理,所以只是遍曆一次。在空間方面因為是邊輸入邊處理,所以不用字串來儲存。    時間:7MS記憶體:196K--------------------------------------------------------------------------------------*/#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 ;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.