SMS

来源:互联网 发布:淘宝信誉131458 编辑:程序博客网 时间:2024/05/18 13:24

1427. SMS

Time limit: 0.5 second
Memory limit: 64 MB

Background

Mobile technologies are going to become a part of our life. So many times you have read this sentence in advertisements and magazine articles. So many times you have heard it from fattened IT corporations presidents who grab money of deceived investors and from managers of mobile phones shops who try to sell useless smartphones at the cost of $500 a piece... Sleep tight. The age of mobility 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 would 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.

Problem

SMS-spam is a promising technology of mass delivery of text advertisements by means of SMS messages. Very convenient, very effective, very easy. Not so easy, however. The problem is the length of one SMS message is limited while 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 for. So an advertisement should be delivered to a thankful recipient by means of minimal number of SMS messages.
And the last thing. Quirky mobile operators have invented 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.

Input

The first line contains the integer numbers 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.

Output

You should 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

/**题目要求:    给定的字符串是一条广告短信,因为字数限制需要分段,然而,分段要满足:    如果某一段只包含字母和空格,那这段长度不能超过M,否则进行分段;    如果某一段不止包含字母和空格,那这段的长度不能超过N(包含非字母和空格的字符),否则进行分段;**/#include<cstdio>#include<cstring>using namespace std;char s[100011];//SMSint dp[100011];/*前n个中非特殊字符(拉丁字符和空格的个数)的个数*/int n,m,res[100011];int main(){    scanf("%d%d",&n,&m);    gets(s);    gets(s+1);    int len=strlen(s+1);    /*初始化,以到i为止的字符串的前缀为状态划分,每段字符串至少分为1段,初始化为1*/    for(int i=1; i<=n; i++)        res[i]=1;    //为了统计长度为i的前缀中特殊字符(非拉丁字符和空格的个数)的个数,    //特殊字符的位置dp=0,则用非特殊字符的位置的dp减去前者,就可以得到长度为i的前缀中特殊字符的个数    for(int i=1; i<=len; i++)        if(s[i]==' '||(s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))        {            if(dp[i-1]+1<=m)                dp[i]=dp[i-1]+1;            else                dp[i]=m;        }        else            dp[i]=0;    /*最终按照特殊字符的个数进行状态划分,只有res[1~n]这n中状态,根据上面的dp,向这n中状态转换*/    for(int i=n+1; i<=len; i++)        if(dp[i]>n)            res[i]=res[i-dp[i]]+1;//i-dp[i]为前i个字符中特殊字符的个数,转换到i-dp[i]的状态        else            res[i]=res[i-n]+1;//同上    printf("%d\n",res[len]);//answer    return 0;}


原创粉丝点击