CodeForces--320A--Magic numbers

Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

[]   [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");
}

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

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

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


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值