CodeForces--320A--Magic numbers

来源:互联网 发布:2017最新网络流行词语 编辑:程序博客网 时间:2024/04/29 01:47
Magic Numbers
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

Description

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. Therefore14144141414 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 Input

Input
114114
Output
YES
Input
1111
Output
YES
Input
441231
Output
NO


CodeForces的题目一向以想法著称,比如这道,题目非常简单:即通过判断1后面的4来进行判断

先贴我丑陋的第一遍代码

//跑的又慢又长!

#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;char a[100];int b[100];int main(){bool flag=true;scanf("%s",a);int length=strlen(a);if(a[0]!='1') flag=false;else{int i;b[0]=1;for(i=1;i<length;++i){if(a[i]!='1'&&a[i]!='4')flag=false;if(a[i]=='1')b[i]=1;}if(flag){for(int j=0;j<length;++j){if(b[j]){for(int k=j+1;;++k){if(b[j]>3){flag=false;break;}if(a[k]=='4') b[j]++;else break;}}if(!flag) break;}}}if(flag) printf("YES\n");else printf("NO\n");return 0;}


再看我通过观察大神们的代码后修改的代码,

又快又精简。

#include <cstdio>#include <cstring>using namespace std;char a[20];int main(){int num=0;scanf("%s",a);for(int i=0;i<strlen(a);++i){if(a[i]=='1')num=2;else if(a[i]=='4')num--;else{printf("NO\n");return 0;}if(num<0){printf("NO\n");return 0;}}printf("YES\n");}

这就是思维的力量啊!!!

很多算法这辈子可能在我未来的工作中难以用到,

可是算法的精髓却会留在我人生未来的每一处地方。


原创粉丝点击