A. ABC String.

原题链接:[https://codeforces.com/contest/1494/problem/A]

题面

You are given a string a, consisting of n characters, n is even. For each i from 1 to n ai is one of ‘A’, ‘B’ or ‘C’.

A bracket sequence is a string containing only characters “(” and “)”. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters “1” and “+” between the original characters of the sequence. For example, bracket sequences “()()” and “(())” are regular (the resulting expressions are: “(1)+(1)” and “((1+1)+1)”), and “)(”, “(” and “)” are not.

You want to find a string b that consists of n characters such that:

b is a regular bracket sequence;
if for some i and j (1≤i,j≤n) ai=aj, then bi=bj.
In other words, you want to replace all occurrences of ‘A’ with the same type of bracket, then all occurrences of ‘B’ with the same type of bracket and all occurrences of ‘C’ with the same type of bracket.

Your task is to determine if such a string b exists.

Input
The first line contains a single integer t (1≤t≤1000) — the number of testcases.

Then the descriptions of t testcases follow.

The only line of each testcase contains a string a. a consists only of uppercase letters ‘A’, ‘B’ and ‘C’. Let n be the length of a. It is guaranteed that n is even and 2≤n≤50.

Output
For each testcase print “YES” if there exists such a string b that:

b is a regular bracket sequence;
if for some i and j (1≤i,j≤n) ai=aj, then bi=bj.
Otherwise, print “NO”.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

Example
input

4
AABBAC
CACA
BBBBAC
ABCA

output
YES
YES
NO
NO

Note
In the first testcase one of the possible strings b is “(())()”.

In the second testcase one of the possible strings b is “()()”.

题解

题意中明确指出找出正则的括号序列。例如,括号序列“()()”和“(())”是正则的(结果表达式是:”(1)+(1)”和“((1+1)+1)”),而“)(”、“(”和“)”不是。

所以第一种情况每次输入的字符串中第一个字母和最后一个字母不能相同,相同的话则不能组成括号序列例如ABA会形成‘()(’ 或者 )()。

如果头字母与尾字母不同时,查找以头字母为(另外不同字母为)数量是否相同,若不同再查找以尾字母为)另外不同字母为(数量是否相同。

代码

#include<iostream>
#include<string.h>
using namespace std;int main()
{int n;cin>>n;while(n--){string a;cin>>a;char s1,s2;int num1=0,num2=0;int f=1;s1=a[0];s2=a[a.size()-1];if(s1==s2)f=0;else{for(int i=0;i<a.size();i++){if(a[i]==s1)num1++;elsenum2++;if(num1<num2){f=0;break;}}if(num1!=num2)f=0;if(f==0){int num3=0,num4=0;for(int i=0;i<a.size();i++){if(a[i]==s2)num3++;elsenum4++;if(num4<num3){f=0;break;}}}}if(f==1)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;
}```

A. ABC String.相关推荐

  1. AtCoder AGC036E ABC String

    题目链接 https://atcoder.jp/contests/agc036/tasks/agc036_e 题解 看了题解第一句话之后意识到这题是sb题以及我又双叒叕智障了-- 首先去掉串中相邻的相 ...

  2. 云端飘 String s=new String(abc)创建了几个对象?

    转自:http://www.cnblogs.com/ydpvictor/archive/2012/09/09/2677260.html -------------------------------- ...

  3. String str =new String(“abc“)和 String str = “abc“的比较

    String是一个非常常用的类,应该深入的去了解String 如: String str =new String("abc") String str1 = "abc&qu ...

  4. String s=new String(abc)创建了2个对象的原因

    问题:String str=new String("abc"); 这行代码究竟创建了几个String对象呢? 相信大家对这道题并不陌生,答案也是众所周知的,2个. 接下来我们就从这 ...

  5. java s1=abc s2=abc s1==s2_经典问题:String s1 = abc 与 String s2 = new String(abc)的区别...

    这是以前刚学Java那会经常被问到的问题,并且也会经常出现在面试中.这个问题不仅仅只局限于Java,可以适用于其他大部分语言,正好最近有个小朋友在学编程,特此整理一下. String s1 = &qu ...

  6. String s1=new String(“abc“); 和String s1=“abc“区别

    在讨论他们的区别时,我们要知道java中的内存,主要分为堆和栈.栈中一般存放的是java中的基本数据变量 比如 int,double,float 还存放了对象的引用变量 如 Person person ...

  7. new string(abc)创建了几个对象_面试题系列第2篇:new String()创建几个对象?有你不知道的...

    Java面试题系列:将面试题中比较经典和核心的内容写成系列文章持续在公众号更新,可巩固基础知识,可梳理底层原理,欢迎大家持续关注[程序新视界].本篇为面试题系列第2篇. 常见面试问题 下面代码中创建了 ...

  8. java中String s=abc及String s=new String(abc)详解

    java中String s="abc"及String s=new String("abc")详解 1.   栈(stack)与堆(heap)都是Java用来在R ...

  9. 怎么做到new String(abc) == new String(abc)双等号为true呢?不是使用equals哦

    正常情况 如果按照正常情况,你面试的时候,看到这个代码,你肯定会说:结果是false. 如果我要他们是true,应该怎么做呢?有什么方法吗? String str4 = new String(&quo ...

最新文章

  1. R语言使用dplyr将特定的数据列移动到最前面、使用dplyr将特定数据列移动到另一指定数据列的后面、使用dplyr将特定数据列移动到另一指定数据列的前面
  2. 实战篇-简单多语言的实现
  3. 计算机学院特色迎新标语,2019大学各学院开学迎新创意标语 2019各大学网红创意迎新宣传标语...
  4. java 基本集合操作
  5. java 中的锁 aqs_Java并发编程系列-(4) 显式锁与AQS
  6. c++ primer打卡(三)
  7. 使用python调用shell判断当前进程是否存在
  8. JTT808协议解析数据相关问题 python
  9. C# XmlDocument.Save文件操作System.IO.IOException:The process cannot access the file because it is being
  10. 2017又是新的一年
  11. 国空三区三线思考之:Arcgis自上而下从左到右进行编号
  12. 倍升工作效率的小策略
  13. mac 挂载 EFI 分区
  14. Android录音转为MP2的实现
  15. 如何从Mac电脑的时间机器中查看恢复特定文件夹备份?
  16. 计算机视觉(CV)方向今年招聘情况怎么样?是否已经人才过剩?
  17. 旅行:旅行的意义是旅行本身没有意义
  18. 如何成为一个优秀的班主任?
  19. 抽奖小程序中用到的播放背景音乐
  20. java利用dropzone多文件上传

热门文章

  1. cts游戏手机版_CTS7遨游中国
  2. 路由转发route详解
  3. PrinTao CANON Home Studio Edition Mac(佳能打印软件) v8.0r12特别版
  4. Generating fantasy maps——来生成虚拟地图吧!【未完】
  5. mysql设置id重新从1开始自增
  6. 网站开发必备图片库推荐
  7. 卡西欧(casio)-SGW-100 磁偏角矫正(电子指南针数码罗盘矫正)
  8. f分布表完整图a=0.01_使用 Infer.NET 评价竞争对手
  9. Sublime Text 3 Version.3143 (序列号失效问题)
  10. Raspberry Pi+Python+微信小程序入门物联网