A == B ?

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 64960    Accepted Submission(s): 10164

Problem Description
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "NO".
 

Input
each test case contains two numbers A and B.
 

Output
for each case, if A is equal to B, you should print "YES", or print "NO".
 

Sample Input

1 2
2 2
3 3
4 3
 

Sample Output
NO
YES
YES
NO




解题心得:
1、这个题很水,不要想得太简单也不要想得太难。就是用数字的规则比较两个字符串是否相等,但是会有小数,还有后导0。
2、其实写这种水题比较考验思维,思维混乱的,写得乱七八糟,要冷静地去思考,考虑是否可以使用stl解决。如果不可以再手动比较,手动比较的时候理清楚思维,不要写得一团乱麻,程序员不要太勤劳,用代码去硬怼题,越写越乱在后面找bug的时候就要炸了。思维清晰,分部分分功能去写。



调用stl(strcmp)的代码:
//使用strcmp,只需要将小数点后面的字符0改为数字0,就可以全部解决了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
char a[maxn],b[maxn]; bool check_pointa()
{
for(int i=0; i<maxn ;i++)
if(a[i] == '.')
return true;
return false;
}
bool check_pointb()
{
for(int i=0; i<maxn ;i++)
if(b[i] == '.')
return true;
return false;
}
int main()
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
while(scanf("%s%s",a,b)!=EOF)
{ bool flaga = false,flagb = false;
//检查是否有小数点,有小数点将小数点后面无用的字符0化为数字0方便后面的比较
flaga = check_pointa();
flagb = check_pointb(); if(flaga)
{
for(int i=maxn-1;i>=0;i--)
{
if(a[i]=='0' || a[i] ==0)
a[i] = 0;
else
{
if(a[i] == '.')
a[i] = 0;
break;
}
}
} if(flagb)
{
for(int i=maxn-1;i>=0;i--)
{
if(b[i]=='0' || b[i] ==0)
b[i] = 0;
else
{
if(b[i] == '.')
b[i] = 0;
break;
}
}
} if(strcmp(a,b) == 0)//直接比较,因为已经将字符0改为了数字0,前导0也就不用管了
printf("YES\n");
else
printf("NO\n");
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}



手动比较的代码:(这个玩意儿复杂死了,都不知道当时自己为什么这么勤快居然老老实实写完了)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
char a[maxn],b[maxn];
int lena,lenb;
int posa,posb; void cmp1()
{
if(lena != lenb)//数位不等直接返回
{
printf("NO\n");
return ;
} for(int i=0; i<lena; i++)//数位相等直接比较
if(a[i] != b[i])
{
printf("NO\n");
return;
}
printf("YES\n");
} void cmp2()
{
if(posb != lena)//b的整数数位比a大,直接返回
{
printf("NO\n");
return;
} for(int i=posb+1; i<lenb; i++)//因为a是整数,只要b的小数部分不全为0直接返回
{
if(b[i] != '0')
{
printf("NO\n");
return ;
}
} for(int i=0; i<posb; i++)//b小数部分为0,整数数位相等,逐位比较就好
if(a[i] != b[i])
{
printf("NO\n");
return;
} printf("YES\n");
} void cmp3()
{
//和cmp2差不多
if(posa != lenb)
{
printf("NO\n");
return;
}
for(int i=posa+1; i<lena; i++)
{
if(a[i] != '0')
{
printf("NO\n");
return ;
}
}
for(int i=0; i<posa; i++)
if(a[i] != b[i])
{
printf("NO\n");
return;
}
printf("YES\n");
} void cmp4()
{
if(posa != posb)//整数数位不相等直接返回
{
printf("NO\n");
return;
} for(int i=0; i<posa; i++)//整数数位相等,逐位比较
if(a[i] != b[i])
{
printf("NO\n");
return;
} int len1 = min(lena - posa - 1,lenb - posb -1);//以较短的那个小数位数的为标准进行诸位比较
for(int i=0; i<len1; i++)
{
if(a[i+posa] != b[i+posb])
{
printf("NO\n");
return;
}
} int len2 = max(lena - posa - 1,lenb - posb - 1);//小数数位比较多的那个多出来的部分不为字符0也不相等
int len = len2 - len1;
if(len2 == lena - posa - 1)//a的位数更多
{
for(int i=0; i<len; i++)
if(a[i+len1+1+posa] != '0')
{
printf("NO\n");
return;
}
} if(len2 == lenb - posb - 1)//b的位数更多
{
for(int i=0; i<len; i++)
if(b[i+len1+1+posb] != '0')
{
printf("NO\n");
return;
}
}
printf("YES\n");
} int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
lena = strlen(a);
lenb = strlen(b);
posa = 0,posb = 0;
bool flaga = false,flagb = false;
int len = max(lena,lenb); //将小数部分和整数部分分开,同时也检查是否有小数部分
for(int i=0; i<len; i++)
{
if(a[i] == '.')
{
posa = i;
flaga = true;
}
if(b[i] == '.')
{
posb = i;
flagb = true;
}
} //都只用整数部分
if(!flaga && !flagb)
cmp1(); //a只有整数部分,b有小数部分
if(!flaga && flagb)
cmp2(); //b只有整数部分,a有小数部分
if(flaga && !flagb)
cmp3(); //都有小数部分
if(flaga && flagb)
cmp4();
}
}




BFS:HDU2054-A==B?(字符串的比较)的更多相关文章

  1. HDU-2054.A==B?(字符串简单处理)

    这道题......被我各种姿势搞死的... 本题大意:给出两个数A和B,判断A和B是否相等,对应输出YES or NO. 本题思路:本题我有两种思路,第一种是直接去除前导零和后导零然后稍加处理比较字符 ...

  2. 洛谷P1032 字串变换【bfs】

    题目链接:https://www.luogu.org/problemnew/show/P1032 题意: 给定一个原字符串和目标字符串,以及几个字符串变换的规则. 问能否根据这几个规则在十步之内把原字 ...

  3. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  4. 【ToolGood.Words】之【StringSearch】字符串搜索——基于BFS算法

    字符串搜索中,BFS算法很巧妙,个人认为BFS算法效率是最高的. [StringSearch]就是根据BFS算法并优化. 使用方法: string s = "中国|国人|zg人|fuck|a ...

  5. P1032 字串变换 字符串BFS

    题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A_2A2​ -> B_2B2​ 规则的含义为:在 AA中的子串 A_1A1​ ...

  6. 【题解】洛谷P1032 [NOIP2002TG]字串变换(BFS+字符串)

    洛谷P1032:https://www.luogu.org/problemnew/show/P1032 思路 初看题目觉得挺简单的一道题 但是仔细想了一下发现实现代码挺麻烦的 而且2002年的毒瘤输入 ...

  7. 字串变换 bfs + 字符串

    题目描述 已知有两个字串A,BA,BA,B及一组字串变换的规则(至多666个规则): A1A_1A1​ ->B1 B_1B1​ A2A_2A2​ -> B2B_2B2​ 规则的含义为:在 ...

  8. 【字符串+BFS】Problem 7. James Bond

    https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个 ...

  9. CodeForces - 1183E Subsequences (easy version) (字符串bfs)

    The only difference between the easy and the hard versions is constraints. A subsequence is a string ...

  10. 【63测试20161111】【BFS】【DP】【字符串】

    第一题: tractor 题目描述 农场上有N(1 <= N <= 50,000)堆草,放在不同的地点上.FJ有一辆拖拉机,也在农场上.拖拉机和草堆都表示为二维平面上的整数坐标,坐标值在1 ...

随机推荐

  1. mysql数据库忘记密码时如何修改(二)

    第一步:找到mysql数据库的my.ini配置文件,在[mysqld]下面添加一行代码:skip-grant-tables 第二步:运行services.msc进入服务管理界面,重启mysql服务. ...

  2. 最简实例演示asp.net5中用户认证和授权(3)

    上接: 最简实例演示asp.net5中用户认证和授权(2) 在实现了角色的各种管理接口后,下一步就是实现对用户的管理,对用户管理的接口相对多一些,必须要实现的有如下三个: 1 public inter ...

  3. MS Chart 折线图——去除时间中的时、分、秒,按天统计【转】

    MS Chart 折线图——去除时间中的时.分.秒,按天统计   private void Form2_Load(object sender, EventArgs e){            str ...

  4. JVM虚拟机 - 内存

    在JVM虚拟机中,内存部分大致可以分为以下几类: Heap:堆 NonHeap:非堆 CodeCache:缓存编辑后的机器码的内存区域 CompressedClassSpace:类压缩空间 MetaS ...

  5. springboot 学习笔记(七)

    (七)springboot整合activemq,消息消费,以及发送对象消息 1.springboot整合activemq发送消息,上一节已经介绍了,现在要对消息队列中的内容进行处理,下面写一个cons ...

  6. Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  7. OpenStack Ocata Telemetry 数据收集服务

    1 安装配置计算服务 在所有计算节点上: 1.1 安装配置组件 安装包: # yum install -y openstack-ceilometer-compute 编辑配置文件/etc/ceilom ...

  8. 学习《CSS选择器Level-4》不完全版

    1 概述 1.1 前言 选择器是CSS的核心组件.本文依据W3C的Selectors Level 4规范,概括总结了Level1-Level4中绝大多数的选择器,并做了简单的语法说明及示例演示.希望对 ...

  9. Counting blessings can actually increase happiness and health by reminding us of the good things in life.

    Counting blessings can actually increase happiness and health by reminding us of the good things in ...

  10. Vue.js(2.x)之Class 与 Style 绑定

    1.前面看数据绑定时还很困惑v-bind处理class时可以使用json格式的值,为什么换成id.href等其他属性就不行.看了下文档解释后明白了些: 2.对象语法主要有以下三种形式: 1)直接在v- ...