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 ...
随机推荐
- B/S模式获取客户端IP地址
using System.Web; namespace Common { public class IPUtil { /// <summary> /// 获取IP地址 /// </s ...
- babel7中 preset-env 完全使用
babel7中 preset-env 完全使用 const presets = [ ['@babel/env', { // chrome, opera, edge, firefox, safari, ...
- git 如何生成 SSH 公钥
1.打开你的git bash 窗口 2.进入.ssh目录:cd ~/.ssh 3.找到id_rsa.pub文件:ls 4.查看公钥:cat id_rsa.pub 或者vim id_rsa.pub ...
- vue列表到详情页的实现
路由里边的 router/index.js path:'/detail/:id' 动态id 列表页渲染时: :to="'/detail/'+item.id" ===>id是指 ...
- Vue打包后页面出现cannot get
学习Vue有大半个月了,然而遇到了不少坑,完全没有高手们那么容易,中间有不少值得记录下的东东,回头好好理理.先理下今天的: Vue打包命令简单啊,直接在命令行输入:npm run build 然而没一 ...
- 解决ie9以及以下console未定义
页面明明已经删除了所有的console,但是ie9下依旧会报错 console未定义 只能这样解决了 window.console = window.console || (function () { ...
- Bugzilla-5.0.3 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.6 bugzilla5.0.3 mariadb5.5.47 perl5.16.3 apache bug tracking bug ...
- iptables (2) 基本配置
iptables 基本命令使用举例 一.链的基本操作 1.清除所有的规则.1)清除预设表filter中所有规则链中的规则.# iptables -F -F, --flush [chain] Flush ...
- Python F-string 更快的格式化
Python的格式化有%s,format,F-string,下面是比较这三种格式化的速度比较 In [12]: a = 'hello' In [13]: b = 'world' In [14]: f' ...
- MRCA|Wright–Fisher population genetic model|SNP rate
(Panda has a high heterozygosity rate) 通过对mtDNA(为了预测SNP的密度)的分析,可知panda的多样性,当前全基因组数据才能完全建立模型. mitocho ...