Crack

1410. Crack

Time limit: 0.5 second
Memory limit: 64 MB
Today Arthur has got not the best day of his life. Today is Thursday and in the morning he had to lie under a bulldozer, which was pulling down his house, and his planet is destroyed, and Marvin is a pain in the neck. And now Arthur is standing in front of Vl'hurgs' commander and he has to speak, otherwise in the space-time continuum a tiny crack will appear and so on... May be exactly Arthur will have this crack. For example, in the bones of his skull. So, he is to speak immediately and certainly in Vl'hurgish. There's only one problem: the only thing that Arthur has heard in Vl'hurgish is the demand to surrender that was proclaimed a second ago. The Hitchhiker's Guide to the Galaxy can only prompt to Arthur that the Vl'hurgish language is very complicated. There are millions of rules and some of them are very amusing. For instance, no Vl'hurgish word can be pronounced for more seconds than the number of letters it consists of. They don't like mumblers. It's known as well that Vl'hurgs don't like when replying to some remark you use any of the two-word combinations that were contained in the original remark. In this case they may think that you parody them and parodists are out of favor among Vl'hurgs. But Arthur is to say something! He has decided to repeat the phrase of Vl'hurgs' commander having thrown out just several words and not changing their order. What else can he do? Just gain time pronouncing each word as long as it is possible.
But how much time can he gain this way?

Input

The input contains a phrase of Vl'hurgs' commander. The words consist just of lower-case and capital Latin letters. Any other symbols are word separators. The phrase doesn't contain two equal words (the case of letters should be ignored). The length of any Vl'hurgish word is not greater than 100. The amount of words in one phrase doesn't exceed 10000. Total input size doesn't exceed 512 KB.

Output

Output a single number: the maximal time in seconds that Arthur may speak. Of course, Arthur doesn't want to seem a mumbler or parodist.

Sample

input output
You have come a long way
11

Hint

In the sample Arthur should say "You come long".
/**
給一個字符串有單詞和標點以及空格組成。在該串中尋找字串。使得該子串有原串種的若干單詞組成,
且子串中兩個相鄰單詞在原串中不能相鄰。原串中的單詞各不相同。求該字串最長多少。

實際上,對於第j個單詞,我們去前j-2箇中的最大dp,再加上第j個單詞的長度,便是第j個單詞位於子串末時的answer。
最後,再枚舉一下所有的j,取dp最大值。

題目的坑在於輸入上~~~
**/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>

using namespace std;

int n;
int len[10010];
int  dp[10010];
int ans;
string s;

void DP()
{
    dp[1]=len[1];
    dp[2]=len[2];
    dp[3]=len[1]+len[3];

    int maxdp=dp[1]<dp[2]?2:1;//本打算練一下單調隊列,發現用不上= =!

    for(int i=4; i<=n; i++)
    {
        dp[i]=dp[maxdp]+len[i];

        if(dp[i-1]>dp[maxdp])
            maxdp=i-1;
    }
    for(int i=1; i<=n; i++)
        ans=max(dp[i],ans);
}

int check(char a)
{
    if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
        return 1;
    return 0;
}

int main()
{
    int sum = 0;
    while(getline(cin,s))//如此讀,因爲會出現以回車作爲間隔符的情況
    {
        s.push_back(' ');
        for (int i=0; i<s.length(); i++)
        {
            if (!check(s[i]))//只記錄字母數
            {
                if (sum)
                {
                    len[++n]=sum;
                    sum = 0;
                }
                continue;
            }
            sum++;
        }
        s.clear();
    }
    DP();
    printf("%d\n",ans);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章