URAL 1427 SMS

URAL 1427

思路:

贪心。

很水的一道贪心,找bug找了很久,没有考虑到n=1的情况。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

char s[100005];
int main(){
    /*ios::sync_with_stdio(false);
    cin.tie(0);*/
    int n,m;
    scanf("%d %d",&n,&m);
    gets(s);
    gets(s);
    bool f=true;
    int cnt=0;
    int tot=0;
    for(int i=0;i<strlen(s);i++){
        if(isalpha(s[i])||s[i]==' ');
        else f=false;
        cnt++;
        if(f){
            if(cnt==m)tot++,cnt=0,f=true;
        }
        else{
            if(cnt==n)tot++,cnt=0,f=true;
            else if(cnt>n)tot++,cnt=1,f=false;
            if(cnt==n)tot++,cnt=0,f=true;//特判n=1
        }
        //cout<<i<<' '<<f<<' '<<cnt<<endl;
    }
    if(f){
        if(0<cnt&&cnt<=m)tot++;
        else if(cnt>m)tot+=2;
    }
    else{
        if(0<cnt&&cnt<=n)tot++;
        else if(cnt>n)tot+=2;
    }
    cout<<tot<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/widsom/p/8406798.html