BFS:HDU2054-A==B?(字符串的比较)
A == B ?
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 64960 Accepted Submission(s): 10164
2 2
3 3
4 3
NO
YES
YES
NO
//使用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?(字符串的比较)的更多相关文章
- HDU-2054.A==B?(字符串简单处理)
这道题......被我各种姿势搞死的... 本题大意:给出两个数A和B,判断A和B是否相等,对应输出YES or NO. 本题思路:本题我有两种思路,第一种是直接去除前导零和后导零然后稍加处理比较字符 ...
- 洛谷P1032 字串变换【bfs】
题目链接:https://www.luogu.org/problemnew/show/P1032 题意: 给定一个原字符串和目标字符串,以及几个字符串变换的规则. 问能否根据这几个规则在十步之内把原字 ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【ToolGood.Words】之【StringSearch】字符串搜索——基于BFS算法
字符串搜索中,BFS算法很巧妙,个人认为BFS算法效率是最高的. [StringSearch]就是根据BFS算法并优化. 使用方法: string s = "中国|国人|zg人|fuck|a ...
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
- 【题解】洛谷P1032 [NOIP2002TG]字串变换(BFS+字符串)
洛谷P1032:https://www.luogu.org/problemnew/show/P1032 思路 初看题目觉得挺简单的一道题 但是仔细想了一下发现实现代码挺麻烦的 而且2002年的毒瘤输入 ...
- 字串变换 bfs + 字符串
题目描述 已知有两个字串A,BA,BA,B及一组字串变换的规则(至多666个规则): A1A_1A1 ->B1 B_1B1 A2A_2A2 -> B2B_2B2 规则的含义为:在 ...
- 【字符串+BFS】Problem 7. James Bond
https://www.bnuoj.com/v3/external/gym/101241.pdf [题意] 给定n个字符串,大小写敏感 定义一个操作:选择任意m个串首尾相连组成一个新串 问是否存在一个 ...
- CodeForces - 1183E Subsequences (easy version) (字符串bfs)
The only difference between the easy and the hard versions is constraints. A subsequence is a string ...
- 【63测试20161111】【BFS】【DP】【字符串】
第一题: tractor 题目描述 农场上有N(1 <= N <= 50,000)堆草,放在不同的地点上.FJ有一辆拖拉机,也在农场上.拖拉机和草堆都表示为二维平面上的整数坐标,坐标值在1 ...
随机推荐
- UVALive 4261——Trip Planning——————【dp+打印路径】
Trip Planning Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Stat ...
- net start命令发生系统错误5和错误1058的解决方法
net start命令用于开启服务,格式为:net start [服务名](与之对应的"net stop [服务名]"为关闭服务命令) 5是没有管理员权限,右键管理员即可 1058 ...
- NeatUpload 的使用
1 <httpModules> 2 <add name="UploadHttpModule" type="Brettle.Web.NeatUpload. ...
- uLua学习之数据交互(三)
前言 在上节中,大概谈了一下如何在lua脚本中调用unity3d中的方法来创建游戏物体,这只是很小的一个方面,uLua的优势在于对unity3d中C#语言的扩展和定制.那么如何扩展和定制呢?其中的数据 ...
- 关于HTML中时间格式以及查询数据库的问题
1.默认时间格式,加入属性dateFormate="yyyy-MM-dd" 2.设置默认值,value="2017-6-22" 3.在JavaScript中将获 ...
- w3cschool中jQuery测试结果总结
1.jQuery 是 W3C 标准. 2.$("div#intro .head") 选择器选取:class="intro" 的任何 div 元素中的首个 id= ...
- docker使用centos7系统构建tomcat镜像
FROM shansongxian/centos-oraclejdk8:latest #此镜像使用centos7系统,精简jdk,只运行java程序,无法编译/构建 MAINTAINER huqian ...
- GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除)
GridView的 PreRender事件与范例--GridView + CheckBox,点选多列资料(复选删除) 之前有一个范例,相同的结果可以用两种作法来实践 [GridView] 资料系结表达 ...
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)
如果你没有看我第二天写的内容的,我想你是看不懂的!!!! 好了,废话不多说,怎么才能让我们的代码变得牛逼起来呢?怎么封装我们的代码呢?我们不可能 每个页面都需要那样写吧,那我们来一步一步来封装 我们的 ...