hdu 3819动态规划
生活随笔
收集整理的這篇文章主要介紹了
hdu 3819动态规划
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
其實(shí)這題算動(dòng)態(tài)規(guī)劃有點(diǎn)牽強(qiáng),因?yàn)樗畲蟮碾y點(diǎn)和突破點(diǎn)在于把問題進(jìn)行轉(zhuǎn)化。我的做法是,先數(shù)出字符串的總數(shù)total和'A'的數(shù)目anum,然后只要在字符串中找出長度為anum的連續(xù)一段,它的'A'數(shù)最多就行了(假設(shè)是maxnum,則最后結(jié)果就是anum-maxnum)。找這個(gè)包含最多'A'的段才用到動(dòng)態(tài)規(guī)劃,其實(shí)挺簡單的,就是循環(huán)一次記錄從每個(gè)點(diǎn)起始往右長度為anum的段中'a'的數(shù)目即可。
/** hdu3819/win.cpp* Created on: 2012-10-29* Author : ben*/ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <queue> #include <set> #include <map> #include <stack> #include <string> #include <vector> #include <deque> #include <list> #include <functional> #include <numeric> #include <cctype> using namespace std; const int MAXN = 100005; char str[MAXN]; int anum, total, ans[MAXN];int main() { #ifndef ONLINE_JUDGEfreopen("data.in", "r", stdin); #endifint T;scanf("%d", &T);getchar();for(int t = 1; t <= T; t++) {printf("Case %d: ", t);gets(str);total = strlen(str);anum = count(str, str + total, 'A');if(anum <= 1) {puts("0");continue;}fill(ans, ans + MAXN, 0);ans[0] = count(str, str + anum, 'A');for(int i = 1; i < total; i++) {ans[i] = ans[i - 1];if(str[i - 1] == 'A') {ans[i]--;}if(str[(i + anum - 1) % total] == 'A') {ans[i]++;}}int result = *max_element(ans, ans + total);printf("%d\n", anum - result); // printf("anum = %d, total = %d\n", anum, total); }return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/moonbay/archive/2012/10/29/2744970.html
總結(jié)
以上是生活随笔為你收集整理的hdu 3819动态规划的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 3646 DP + 二分
- 下一篇: 按键中断异步通知实现