cf 189 div2 A - Magic Numbers

A. Magic Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A magic number is a number formed by concatenation of numbers 114 and 144. We can use each of these numbers any number of times. Therefore 14144141414 and 1411 are magic numbers but 1444514 and 414 are not.

You're given a number. Determine if it is a magic number or not.

Input

The first line of input contains an integer n(1 ≤ n ≤ 109). This number doesn't contain leading zeros.

Output

Print "YES" if n is a magic number or print "NO" if it's not.

Sample test(s)
input
114114
output
YES
input
1111
output
YES
input
441231
output
NO
 
    
水题。下面是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define inf 1000000000
using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    char s[100];
    cin>>s;
    int i,j;
    int l=strlen(s);
    for(i=0;i<l;i++){
         if(s[i]!='1'&&s[i]!='4')
            break;
    }
    if(i<l||s[0]!='1'){
         cout<<"NO"<<endl;
         return 0;
    }
    for(i=0;i<l;i++){
         if(s[i]!='1') continue;
         for(j=i+1;j<l;j++){
             if(s[j]=='1') break;
         }
          if(j-i>3){
             cout<<"NO"<<endl;
             return 0;
          }
    }
    cout<<"YES"<<endl;
    return 0;
}


你可能感兴趣的:(cf 189 div2 A - Magic Numbers)