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 ...
随机推荐
- [Coding Style] CSS coding style
CSS coding style Note 结合实际工作中的规范和推荐大家使用的CSS书写规范.顺序这篇文章整合而成 Navigation CSS 书写顺序 CSS 常用文件命名 CSS 常用命名规范 ...
- *.rpm is not signed解决
1.# yum install qemu*报错如下:Package qemu-kvm-tools-0.12.1.2-2.113.el6.x86_64.rpm is not signed2.解决# vi ...
- linux性能测试脚本
http://linux-bench.com/ What is Linux-Bench? Linux-Bench is a simple script that provides a basic le ...
- Weblogic 10.3.6.0 集群搭建
Weblogic 集群搭建 Oracle的Weblogic分开发者版本和生产版本,有32位和64位.一般生产版本的weblogic是64位的,安装文件是一个大小为1G多的jar包.去oracle官网上 ...
- kubernetes发布解释型语言应用的最佳实践
说明 k8s在发布编译型语言的应用时,几乎不用多考虑,就会选择将编译好jar/war包(java语言)或者二进制文件(golang/c++)直接打到镜像当中,生成新的应用镜像,然后将镜像推到镜像仓库, ...
- hihocoder 第四十周 三分求极值
题目链接:http://hihocoder.com/contest/hiho40/problem/1 ,一道简单的三分. 题目是在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求 ...
- 工作流性能优化(敢问activiti有扩展性?)(2)
2015/4/17 粗略看了activiti的sql的,在ativity engine包里边: 没什么头绪,先用excel记录数据量少的时候本机的性能情况: 不打印hibernate的sql:一刷 ...
- IOS plist的数据 存 取(沙河目录)
应用沙盒目录的常见获取方式 沙盒根目录:NSString *home = NSHomeDirectory(); Documents:(2种方式) 1.利用沙盒根目录拼接”Documents”字符串 N ...
- Dynamic typing 动态类型
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Dyna ...
- vuejs属性绑定和双向绑定
属性绑定 html <div v-bind:title="title">hello world</div> js new Vue({ el:'#root', ...