zoukankan      html  css  js  c++  java
  • CodeForces 550A Two Substrings(模拟)

    【题目链接】click here~~ 
    【题目大意】: 
    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 
    给出一行字符,推断是否出现两个不重叠的字串“AB”和“BA” 

    代码:

    /*
    str.find()函数
    返回str在字符串中第一次出现的位置(从index開始查找)。假设没找到则返回string::npos, 
    返回str在字符串中第一次出现的位置(从index開始查找。长度为length)。

    假设没找到就返回string::npos, 返回字符ch在字符串中第一次出现的位置(从index開始查找)。假设没找到就返回string::npos */ #include <bits/stdc++.h> using namespace std; string str; int main() { cin>>str; int len=str.size(); if(len<=3) puts("NO"); else { int a=str.find("AB"); int b=str.find("BA",a+2); int c=str.find("BA"); int d=str.find("AB",c+2); if(a!=-1&&b!=-1||c!=-1&&d!=-1) puts("YES"); else puts("NO"); } return 0; } /* strstr函数:查找字符串第一次出现的位置 */ #include<bits/stdc++.h> using namespace std; char str[200000],*p; int main() { cin>>str; if((p=strstr(str,"AB")) && (strstr(p+2,"BA")))puts("YES"); else if((p=strstr(str,"BA")) && strstr(p+2,"AB"))puts("YES"); else puts("NO"); return 0; }



  • 相关阅读:
    三: 视图容器。
    (微信小程序)一 : 初识微信小程序
    Centos 从零开始 (四)
    Centos 从零开始 (三)
    Centos 从零开始 (二)
    Centos 从零开始 (一)
    9、下拉刷新 与 上拉加载
    8、导航:Nav
    C#异步编程 Task await的理解
    WPF 获取主线程
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6696236.html
Copyright © 2011-2022 走看看