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 ...
随机推荐
- electron 集成 SQLCipher
mac 安装 brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...
- C#获得字符串首字符字母(大写)
/// <summary> /// 获得字符串首字符字母(大写): /// </summary> /// <param name="cnChar"&g ...
- VS2008 Pocket PC 2003 SE VGA仿真程序网络设置
最近对这个问题摸索的很久,都没有解决,今天终于搞定,现将大体设置步骤记录下来,以备回顾和方便别人查看,步骤如下: 1.打开VS2008,打开Windows Mobile设备中心(网上有下载). 2.连 ...
- springmvc之Hello World及常用注解
步骤: 加入jar包 在web.xml 中配置DispacherServlet 加入SpringMVC 配置文件springmvc.xml 编写请求处理器(action/controller) 编写视 ...
- tomcat的备份脚本
reference:Crontab的20个例子 先科普一下date的使用方法,在sh脚本中经常会使用得到 date -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: ...
- 零基础逆向工程25_C++_02_类的成员权限_虚函数_模板
1 类的成员权限 1.1 小结: 1.对外提供的函数或者变量,发布成public的 但不能随意改动. 2.可能会变动的函数或者变量,定义成private的 这样编译器会在使用的时候做检测. 3.只有结 ...
- OpenFirewall
1.写一份json文件:将要添加防火墙例外的应用程序和端口写入到json文件中 2.打开防火墙,读取json文件添加例外 /// <summary> /// Firewall.xaml 的 ...
- dac verilog ad5601
首先从官网下载数据手册.DAC有串行有并行,ad5601是串行,(需要好多时钟沿的移位内部转换为并行在输出). 按照手册的时序编写程序, 关注下芯片的波特率范围 看看手册的数据传输那些事有效的数据位 ...
- codevs 2620 战壕
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description Smart和Sarah正在玩一个即时战略游戏. Smart在他的基地附近建立了n个 ...
- signal函数:void (*signal(int,void(*)(int)))(int);
http://blog.chinaunix.net/uid-20178794-id-1972862.html signal函数:void (*signal(int,void(*)(int)))(int ...