Ural 1427. SMS (linear DP)

Source: Internet
Author: User

The question is: give you a string, and then let you cut it to at least a few points. If only uppercase/lowercase letters and spaces are contained, a maximum of M characters can be taken into one. If there are other characters, a maximum of n characters can be taken into one.

Many direct DP [I] [J] data cannot be stored in space or time. DP [I] [0] indicates only letters and spaces, and DP [I] [1] indicates other characters. Vis [I] [0], vis [I] [1] dynamic record length. The time will be reduced to O (n) space to 2 * n.

1427. smstime limit: 0.5 second
Memory limit: 64 MB
Backgroundmobile technologies are going to become a part of our life. so many times you have read this sentence in advertisements and magazine articles. so frequently times you have heard it from fattened it deployments ations presidents who grab money of deceived investors and from managers of mobile phones shops who try to launch useless smartphones at the cost of $500 a piece. .. sleep tight. the age of each I Ty has not begun yet. believe me, you will feel when it comes to life. one day it will be felt by the millions of people who wocould find their mobile phones full of dozens of SMS messages offering sweets with swastika, courses of American Russian, services of famous charlatan Ilya German and participation in forthcoming contests on timus online judge. unfortunately the history will not keep the name Of one modest programmer who was in the very origin of New Age technology which will be known soon as SMS-spam. but I will say something else. this programmer is you. problemsms-spam is a promising technology of mass delivery of text advertisements by means of SMS messages. very convenient, very valid tive, very easy. not so easy, however. the problem is the length of one SMS message is limited whi Le advertisements are usually rather long. fortunately, an advertisement can be divided into several parts, and each part will be sent as a separate SMS message. but here greedy mobile operators enter the game, because they also want to get some money. their acquisitiveness is expressed in the fact that each delivered SMS message must be paid. so an advertisement shocould be delivered to a thankf Ul recipient by means of minimal number of SMS messages. and the last thing. quirky mobile operators have specified Ted an amusing feature for people who want to save some money. SMS message which consists of Latin letters and spaces only can be up to M characters long while the length of SMS message which consists of any characters is limited by n characters. inputthe first line contains the integer num Bers N and M (1 ≤ n ≤m ≤ 10000 ). the second line contains an advertisement. the advertisement consists of from 1 to 100000 characters. each character is either a Latin letter, a space, a digit or a punctuation mark ". "(full stop),", "(comma),"; "(semicolon),": "(colon ),"! "(Exclamation mark ),"? "(Question mark),"-"(hyphen) or" (double quotes ). the advertisement is terminated by the end of line. outputyou shoshould output the minimal number of SMS messages required to deliver the advertisement. sample
Input
10 15On the 11-th of February, 2006 the contest "Timus Top Coders: First Challenge" is held!
Output
8

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-12///#define M 1000100#define LL __int64///#define LL long long///#define INF 0x7ffffff#define INF 0x3f3f3f3f#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)using namespace std;const int maxn = 100010;int dp[maxn][2];int vis[maxn][2];int judge(char s){    if(s == ' ') return 1;    if(s <= 'Z' && s >= 'A') return 1;    if(s <= 'z' && s >= 'a') return 1;    return 0;}char str[maxn];int main(){    int n, m;    while(cin >>n>>m)    {        memset(dp, 0, sizeof(dp));        memset(vis, 0, sizeof(vis));        getchar();        gets(str);        int len = strlen(str);        dp[0][1] = 1;        vis[0][1] = 1;        if(judge(str[0]))        {            dp[0][0] = 1;            vis[0][0] = 1;        }        else        {            dp[0][0] = -1;            vis[0][0] = 0;        }        for(int i = 1; i < len; i++)        {            if(judge(str[i]))            {                if(vis[i-1][0] >= m)                {                    dp[i][0] = min(dp[i-1][1], dp[i-1][0])+1;                    vis[i][0] = 1;                }                else                {                    if(vis[i-1][0] && dp[i-1][0] < dp[i-1][1]+1)                    {                        dp[i][0] = dp[i-1][0];                        vis[i][0] = vis[i-1][0]+1;                    }                    else                    {                        dp[i][0] = dp[i-1][1]+1;                        vis[i][0] = 1;                    }                }            }            else                vis[i][0] = 0;            if(vis[i-1][1] >= n)            {                if(vis[i-1][0]) dp[i][1] = min(dp[i-1][1], dp[i-1][0])+1;                else dp[i][1] = dp[i-1][1]+1;                vis[i][1] = 1;                continue;            }            if(!vis[i-1][0] || dp[i-1][1] < dp[i-1][0]+1)            {                dp[i][1] = dp[i-1][1];                vis[i][1] = vis[i-1][1]+1;                continue;            }            dp[i][1] = dp[i-1][0]+1;            vis[i][1] = 1;        }        if(vis[len-1][0]) cout<<min(dp[len-1][0], dp[len-1][1])<<endl;        else cout<<dp[len-1][1]<<endl;    }    return 0;}


Ural 1427. SMS (linear DP)

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.