A. Two Substrings
2015-07-03 19:01
459 查看
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given string s. Your task is to determine if the given string s contains
two non-overlapping substrings "AB" and "BA"
(the substrings can go in any order).
Input
The only line of input contains a string s of length between 1 and 105 consisting
of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains
two non-overlapping substrings "AB" and "BA",
and "NO" otherwise.
Sample test(s)
input
output
input
output
input
output
Note
In the first sample test, despite the fact that there are substrings "AB" and "BA",
their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
解题说明:此题其实就是判断字符串中是否包含某个特定的子串,用strstr函数即可。另外注意要求不存在叠加的情况,可以用指针实现移位。
#include<stdio.h>
#include <string.h>
char s[100001];
int main()
{
char *c;
scanf("%s",s);
if((c=strstr(s,"AB"))!=NULL && strstr(c+2,"BA")!=NULL)
{
printf("YES\n");
return 0;
}
if((c=strstr(s,"BA"))!=NULL && strstr(c+2,"AB")!=NULL)
{
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given string s. Your task is to determine if the given string s contains
two non-overlapping substrings "AB" and "BA"
(the substrings can go in any order).
Input
The only line of input contains a string s of length between 1 and 105 consisting
of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains
two non-overlapping substrings "AB" and "BA",
and "NO" otherwise.
Sample test(s)
input
ABA
output
NO
input
BACFAB
output
YES
input
AXBYBXA
output
NO
Note
In the first sample test, despite the fact that there are substrings "AB" and "BA",
their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
解题说明:此题其实就是判断字符串中是否包含某个特定的子串,用strstr函数即可。另外注意要求不存在叠加的情况,可以用指针实现移位。
#include<stdio.h>
#include <string.h>
char s[100001];
int main()
{
char *c;
scanf("%s",s);
if((c=strstr(s,"AB"))!=NULL && strstr(c+2,"BA")!=NULL)
{
printf("YES\n");
return 0;
}
if((c=strstr(s,"BA"))!=NULL && strstr(c+2,"AB")!=NULL)
{
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
相关文章推荐
- HBase源码学习 客户端scan过程
- SQL中Group By的使用
- 数据库事务(Database Transaction)
- 数据结构—基础知识
- Android Design Support Library学习
- day 2
- Android中设置全屏的方法
- 5.19
- oracle中schema指的是什么?
- jsp语法
- Swift实现类网易云音乐横向scroll menu切换菜单(一)
- web应用前后分离的尝试
- 没发现这么好的地方
- MFC对注册表的操作
- Nginx Upstream Keepalive 分析
- 银行取款[多线程]{使用ThreadLocal管理共享变量,但此场景并不保证线程同步}
- 玩转CPU Topology
- HDU 2896
- 关于numa的一些总结
- Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟