CF1547B Alphabetical Strings(了解字符串的的一些规律)

简介: CF1547B Alphabetical Strings(了解字符串的的一些规律)

Alphabetical Strings



time limit per test2 seconds

memory limit per test512 megabytes

inputstandard input

outputstandard output


A string s of length n (1≤n≤26) is called alphabetical if it can be obtained using the following algorithm:


first, write an empty string to s (i.e. perform the assignment s := “”);

then perform the next step n times;


at the i-th step take i-th lowercase letter of the Latin alphabet and write it either to the left of the string s or to the right of the string s (i.e. perform the assignment s := c+s or s := s+c, where c is the i-th letter of the Latin alphabet).


In other words, iterate over the n first letters of the Latin alphabet starting from ‘a’ and etc. Each time we prepend a letter to the left of the string s or append a letter to the right of the string s. Strings that can be obtained in that way are alphabetical.


For example, the following strings are alphabetical: “a”, “ba”, “ab”, “bac” and “ihfcbadeg”. The following strings are not alphabetical: “z”, “aa”, “ca”, “acb”, “xyz” and “ddcba”.


From the given string, determine if it is alphabetical.


Input

The first line contains one integer t (1≤t≤10^4) — the number of test cases. Then t test cases follow.


Each test case is written on a separate line that contains one string s. String s consists of lowercase letters of the Latin alphabet and has a length between 1 and 26, inclusive.


Output

Output t lines, each of them must contain the answer to the corresponding test case. Output YES if the given string s is alphabetical and NO otherwise.


You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive answer).


Example


inputCopy

11

a

ba

ab

bac


ihfcbadeg

z

aa

ca

acb

xyz

ddcba


outputCopy

YES

YES

YES

YES

YES

NO

NO

NO

NO

NO

NO


Note

The example contains test cases from the main part of the condition.


题意分析


生成一个字符串(字符都是小写字母)。生成规则如下:


·初始字符串是空的。


·每次在前面或后面加入一个字符,第 ii 次加入的字符是小写字母表中第 ii 个小写字母。


给一些字符串,请判断是否可以被生成出来。


做法



显然若字符串中字符的出现并不连续或者同一个字符出现了两次,那么该串是不行的。


举个栗子:dba,bab。


然后我们看:每次字母从小到大加,且加在两侧,那是不是中间的字符会比旁边的小呢?


有了这些结论,这题就做完了,上代码。

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    string s;
    cin>>s;
    int l=0;
    int r=s.size()-1;
    while(l<=r)
    {
      if((s[l]-'a')==r-l)
      l++;
      else if((s[r]-'a')==r-l)
      r--;
      else break; 
    }
    if(l>r)
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl; 
  }
 } 


相关文章
语雀的markdown常用语法
语雀的markdown常用语法
8248 0
语雀的markdown常用语法
|
11月前
|
人工智能 算法 物联网
Lyra:SmartMore 联合香港多所高校推出的多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力
Lyra是由香港中文大学、SmartMore和香港科技大学联合推出的高效多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力。Lyra基于开源大型模型和多模态LoRA模块,减少训练成本和数据需求,支持多种模态理解和推理任务。
378 33
Lyra:SmartMore 联合香港多所高校推出的多模态大型语言模型,专注于提升语音、视觉和语言模态的交互能力
|
5月前
|
C++
C++编程技巧:sort()函数中的greater<int>()参数使用讲解
所以你看,`sort()`函数和 `greater<int>()`的组合就像一个魔法工具箱,可以轻松地完成从大到小的排序任务。希望这个小小的技巧能为你的C++编程之路增添一份乐趣。
218 23
广义表,广义表的定义和计算
广义表的定义和概念,包括空表、单元素表、嵌套子表以及如何计算广义表的长度、取表头、取表尾和计算广义表的深度。
455 1
|
9月前
|
人工智能 JavaScript 关系型数据库
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
360 14
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
|
9月前
|
网络安全 开发工具 git
解决 Git 访问 GitHub 时的 SSL 错误
通过上述步骤,可以有效解决 Git 访问 GitHub 时的 SSL 错误。推荐优先更新 CA 证书和正确配置 Git 使用 CA 证书,避免禁用 SSL 验证。如果问题持续,可以切换到 SSH 方式访问 GitHub,确保连接的安全性和稳定性。希望这些内容对您的学习和工作有所帮助。
3028 4
|
计算机视觉 Python
np.ones
np.ones
409 1
|
存储 程序员 C++
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
670 0
|
开发工具 git
完美解决 fatal: unable to access ‘https://github.com/.../.git‘: Could not resolve host: github.com
完美解决 fatal: unable to access ‘https://github.com/.../.git‘: Could not resolve host: github.com
35657 1
|
机器学习/深度学习 存储 算法
【Deep Learning 7】RNN循环神经网络
🍊本文旨在以一种通俗易懂的方式来介绍RNN,本文从天气预报开始引入RNN,随后对RNN进行详细介绍,并对几种变体的LSTM、CRU、BiLSTM进行重点介绍。最后以5个实验进行实战训练 🍊实验一:Pytorch+Bert+RNN实现文本分类预测模拟 🍊实验二:Pytorch+Bert+RNN实现对IMDB影评数据集进行二分类情感分析 🍊实验三:Pytorch+Bert+GRU实现对IMDB影评数据集进行二分类情感分析 🍊实验四:Pytorch+Bert+LSTM实现对IMDB影评数据集进行二分
408 1