新用户注册入口 老用户登录入口

[转载]Codeforces Round #712 (Div. 2)-ABC

文章作者:转载 更新时间:2023-10-05 13:54:12 阅读数量:227
文章标签:回文字符串字符串Déjà Vu非回文字符串字符串操作回文字符串
本文摘要:这篇文章探讨了一道编程题,题目要求在给定的字符串中插入字符 'a' ,使得结果不再是回文串。通过分析字符串特性发现,只有全由字符 'a' 组成的回文串无法满足条件。对于其他情况,可以在字符串首或尾尝试添加 'a' 并检查是否为回文,从而构造出非回文解。文章详细阐述了如何通过遍历和判断字符串中的字符来实现这一目标,并提供了相应的C++代码示例。关键词包括:回文字符串、插入字符 'a'、字符串操作、YES/NO 输出格式、Déjà Vu、 palindrome、非回文字符串、TF-IDF算法(虽未深入讨论但可能涉及)以及加权算法(权重计算)。
转载文章

本篇文章为转载内容。原文链接:https://blog.csdn.net/lvy_yu_ET/article/details/115575091。

该文由互联网用户投稿提供,文中观点代表作者本人意见,并不代表本站的立场。

作为信息平台,本站仅提供文章转载服务,并不拥有其所有权,也不对文章内容的真实性、准确性和合法性承担责任。

如发现本文存在侵权、违法、违规或事实不符的情况,请及时联系我们,我们将第一时间进行核实并删除相应内容。

A. Déjà Vu
A palindrome is a string that reads the same backward as forward. For example, the strings “z”, “aaa”, “aba”, and “abccba” are palindromes, but “codeforces” and “ab” are not. You hate palindromes because they give you déjà vu.

There is a string s
. You must insert exactly one character ‘a’ somewhere in s

. If it is possible to create a string that is not a palindrome, you should find one example. Otherwise, you should report that it is impossible.

For example, suppose s=

“cbabc”. By inserting an ‘a’, you can create “acbabc”, “cababc”, “cbaabc”, “cbabac”, or “cbabca”. However “cbaabc” is a palindrome, so you must output one of the other options.
Input

The first line contains a single integer t
(1≤t≤104

) — the number of test cases.

The only line of each test case contains a string s

consisting of lowercase English letters.

The total length of all strings does not exceed 3⋅105

.
Output

For each test case, if there is no solution, output “NO”.

Otherwise, output “YES” followed by your constructed string of length |s|+1

on the next line. If there are multiple solutions, you may print any.

You can print each letter of “YES” and “NO” in any case (upper or lower).
Example
Input
Copy

6
cbabc
ab
zza
ba
a
nutforajaroftuna

Output
Copy

YES
cbabac
YES
aab
YES
zaza
YES
baa
NO
YES
nutforajarofatuna

Note

The first test case is described in the statement.

In the second test case, we can make either “aab” or “aba”. But “aba” is a palindrome, so “aab” is the only correct answer.

In the third test case, “zaza” and “zzaa” are correct answers, but not “azza”.

In the fourth test case, “baa” is the only correct answer.

In the fifth test case, we can only make “aa”, which is a palindrome. So the answer is “NO”.

In the sixth test case, “anutforajaroftuna” is a palindrome, but inserting ‘a’ elsewhere is valid.
题意:
给你一个字符串,然后你可以通过在任意位置上+‘a’,然后让这个字符串不是回文字符串,如果实在无法让字符串变成非回文,则输出NO。
思路:
我们可以判断出只有全为a的回文字符串才会+‘a’后无法变成非回文串,其他的都可以在任意位置上“a”,使字符串变成非回文串,那我们可以直接在首或者尾“a”,然后循环判断是否为回文,输出非回文的那一种就可以了。
代码:

#include<bits/stdc++.h>
using namespace std;
bool judge(string p)
{for(int i=0,j=p.size()-1;i<j;i++,j--){if(p[i]!=p[j])return true;}return false;
}
int main ()
{int t;cin>>t;while(t--){string ch;cin>>ch;int f=0;for(int i=0;i<ch.size();i++){if(ch[i]!='a'){f=1;break;} }if(f==0){cout<<"NO"<<endl;}else{string ch1,ch2;ch1="a"+ch;ch2=ch+"a";if(judge(ch1)){cout<<"YES"<<endl;cout<<'a';for(int i=0;i<ch.size();i++) cout<<ch[i];cout<<endl;}else{if(judge(ch2)) {cout<<"YES"<<endl;for(int i=0;i<ch.size();i++) cout<<ch[i];cout<<'a';cout<<endl;}elsecout<<"NO"<<endl;} }}return 0;
}

B. Flip the Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a binary string a
of length n. In one operation, you can select any prefix of a with an equal number of 0 and 1 symbols. Then all symbols in the prefix are inverted: each 0 becomes 1 and each 1 becomes 0
For example, suppose a=0111010000
since it has four 0’s and four 1’s: [01110100]00→[10001011]00
In the second operation, we can select the prefix of length 2
since it has one 0 and one 1: [10]00101100→[01]00101100
It is illegal to select the prefix of length 4
for the third operation, because it has three 0’s and one
Can you transform the string a
into the string b
using some finite number of operations (possibly, none)?
Input
The first line contains a single integer t
(1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n
(1≤n≤3⋅105) — the length of the strings a and b
The following two lines contain strings a
and b of length n, consisting of symbols 0 and 1
The sum of n
across all test cases does not exceed 3⋅105
Output
For each test case, output “YES” if it is possible to transform ainto b, or “NO” if it is impossible. You can print each letter in any case (upper or lower).
Example
Input
Copy
5
10
0111010000
0100101100
4
0000
0000
3
001
000
12
010101010101
100110011010
6
000111
110100

Output
Copy

YES
YES
NO
YES
NO

Note

The first test case is shown in the statement.

In the second test case, we transform a
into b

by using zero operations.

In the third test case, there is no legal operation, so it is impossible to transform a
into b

.

In the fourth test case, here is one such transformation:

Select the length 2

prefix to get 100101010101
.
Select the length 12
prefix to get 011010101010
.
Select the length 8
prefix to get 100101011010
.
Select the length 4
prefix to get 011001011010
.
Select the length 6
prefix to get 100110011010

In the fifth test case, the only legal operation is to transform a
into 111000. From there, the only legal operation is to return to the string we started with, so we cannot transform a into b
题意:
给你一个字符串a,b,由0,1组成,然后只有在字符串下标i前面的0,1个数相同时,你可以进行把0->1,1->0,然后看是否能进行一些操作把字符串a变成b。
思路:
这题思路有点难想,你看,它每次个数相同时,都可以进行操作,所以我们从后往前进行操作,因为如果从前往后,小区间会影响大区间的,所以从大区间向小区间进行,然后遍历字符串,将每一位的0,1的个数进行计算,然后将a,b不相同的下标进行标记为1,代表需要改变。

从后遍历,cnt进行次数,因为如果是奇数的话,才会变成不一样的数字,偶数的话,区间变化会使它变回去了,在判断当前位之前,我们先看之前的大区间的变化将当前第i位变成了什么,因为只有0,1,跟标记的0,1是代表他是否需要变化,假如说:原先为0,他后面的区间将它变化了奇数次,那么它就现在是需要变化的才能变成吧b。原先为1,它后面的区间将它变化了偶数次,就还是1。如果这个下标的标记为1,代表需要被变化,我们就判断当前位的0,1个数是否相同,相同的话就代表了一次变化cnt++,否则就退出无法变成b了,因为之后没有区间将它再次变化了。然后我们就可以判断了
代码:

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+7;
int s0[N],s1[N];
int a[N],b[N],p[N];
int main ()
{int t;cin>>t;while(t--){int n;cin>>n;string a,b;cin>>a>>b;memset(p,0,sizeof p);for(int i=0;i<n;i++){if(a[i]=='0'){s0[i]=s0[i-1]+1;s1[i]=s1[i-1];}else{s1[i]=s1[i-1]+1;s0[i]=s0[i-1];}if(a[i]!=b[i]){p[i]=1;//是否相同的标记} }int cnt=0;int f=0;for(int i=n-1;i>=0;i--){if(cnt%2==1){//奇数次才会被变化p[i]=1-p[i];}//而且必须在前面判这一步,因为你得先看后面的区间将这一位变成了什么if(p[i]){if(s0[i]==s1[i]) cnt++;//0,1相同时才可以进行一次变化else {f=1;break;} }}if(f==1){cout<<"NO"<<endl;}else{cout<<"YES"<<endl;} }return 0;
}//这个就利用了一个标记来判断当前为被影响成了什么
/*
01 01 01 01 01 01
10 01 10 01 10 10100101010101
011010101010
100101011010
011001011010
100110011010
Select the length 12
prefix to get
.
Select the length 8
prefix to get
.
Select the length 4
prefix to get
.
Select the length 6
prefix to get01 110100 00
01 001011 00
*/

C. Balance the Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters ‘+’ and ‘1’. For example, sequences ‘(())()’, ‘()’, and ‘(()(()))’ are balanced, while ‘)(’, ‘(()’, and ‘(()))(’ are not.

You are given a binary string s
of length n. Construct two balanced bracket sequences a and b of length n such that for all 1≤i≤n if si=1, then ai=bi
if si=0, then ai≠bi
If it is impossible, you should report about it.
Input
The first line contains a single integer t
(1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n
(2≤n≤2⋅105, nis even).
The next line contains a string sof length n, consisting of characters 0 and 1.The sum of nacross all test cases does not exceed 2⋅105.
Output
If such two balanced bracked sequences exist, output “YES” on the first line, otherwise output “NO”. You can print each letter in any case (upper or lower).
If the answer is “YES”, output the balanced bracket sequences a and b satisfying the conditions on the next two lines.If there are multiple solutions, you may print any.
Example
Input
Copy
3
6
101101
10
1001101101
4
1100
Output
Copy
YES
()()()
((()))
YES
()()((()))
(())()()()
NO
Note
In the first test case, a=
“()()()” and b="((()))". The characters are equal in positions 1, 3, 4, and 6, which are the exact same positions where si=1
.In the second test case, a=
“()()((()))” and b="(())()()()". The characters are equal in positions 1, 4, 5, 7, 8, 10, which are the exact same positions where si=1
In the third test case, there is no solution.
题意:
一个n代表01串的长度,构造两个长度为n的括号序列,给你一个01串,代表着a,b两个序列串字符不相同。然后你来判断是否有合理的a,b串。有的话输出。
思路:
这题想了很久想不明白,看了大佬的题解,迷迷糊糊差不多理解吧。这题是这样的,就是:
(1)第一步得合法的字符串,所以首尾得是相同的且都为1
(2)第二步,因为01串长度为偶数,所以如果合法的话,得( 的个数= ) 的个数,然后你想呀,假如为 ()()()()吧,然后你有一个0破坏了一个括号,但如果合法的话,是不是得还有一个0再破坏一个括号,然后被破坏的这俩个进行分配才能合理,所以如果合法的话,01串得0的个数为偶数,1的个数自然而然为偶数吧。
(3)最后一步构造,既然1的个数为偶数,首尾又都为1,所以1的个数前sum1/2个1构造为‘( ’,后sum1/2个构造为‘)’,然后我们1的所有的目前是合法的,然后剩下的0也是偶数的,然后如果让他们合法进行分配就( )间接进行就可以了,然后我们根据01串将b构造出来。合法的核心就是当前位的(个数大于等于),所以我们在循环进行判断一下a,b串是否都满足,(其实我觉得这么构造出来,a必然合理呀,其实就判b就行了,我保险起见都判了)。
代码:

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+7;
char a[N],b[N];
int main ()
{int t;cin>>t;while(t--){int n;cin>>n;string s;cin>>s;if(s[0]!=s[n-1]&&s[0]!='1'){cout<<"NO"<<endl;}else{int sum1=0,sum0=0;for(int i=0;i<s.size();i++){if(s[i]=='1') sum1++;else sum0++;}if(sum1%2!=0||sum0%2!=0){cout<<"NO"<<endl;}else{int cnt1=0,cnt0=1;for(int i=0;i<n;i++){if(s[i]=='1'&&cnt1<sum1/2){a[i]='(';cnt1++;}else if(s[i]=='1'&&cnt1>=sum1/2){a[i]=')';cnt1++;}else if(s[i]=='0'&&cnt0%2==1){a[i]='(';cnt0++;}else if(s[i]=='0'&&cnt0%2==0){a[i]=')';cnt0++;}//cout<<a[i]<<endl;}for(int i=0;i<n;i++){if(s[i]=='0'){if(a[i]=='(') b[i]=')';else b[i]='(';}else{b[i]=a[i];}//cout<<b[i]<<endl;}// cout<<"YES"<<endl;int f=0;int s0=0,s1=0;for(int i=0;i<n;i++){if(a[i]=='(') s0++;else if(a[i]==')') s1++;if(s0<s1) {f=1;break;} }s0=0,s1=0;for(int i=0;i<n;i++){if(b[i]=='(') s0++;else if(b[i]==')') s1++;if(s0<s1) {f=1;break;} }if(f==0){cout<<"YES"<<endl;for(int i=0;i<n;i++) cout<<a[i];cout<<endl;for(int i=0;i<n;i++) cout<<b[i];cout<<endl;}else{cout<<"NO"<<endl;} }} }return 0;
}
/*
01 01 01 01 01 01
10 01 10 01 10 10100101010101
011010101010
100101011010
011001011010
100110011010
Select the length 12
prefix to get
.
Select the length 8
prefix to get
.
Select the length 4
prefix to get
.
Select the length 6
prefix to get01 110100 00
01 001011 00
*/

本篇文章为转载内容。原文链接:https://blog.csdn.net/lvy_yu_ET/article/details/115575091。

该文由互联网用户投稿提供,文中观点代表作者本人意见,并不代表本站的立场。

作为信息平台,本站仅提供文章转载服务,并不拥有其所有权,也不对文章内容的真实性、准确性和合法性承担责任。

如发现本文存在侵权、违法、违规或事实不符的情况,请及时联系我们,我们将第一时间进行核实并删除相应内容。

相关阅读
文章标题:[转载][洛谷P1082]同余方程

更新时间:2023-02-18
[转载][洛谷P1082]同余方程
文章标题:[转载]webpack优化之HappyPack实战

更新时间:2023-08-07
[转载]webpack优化之HappyPack实战
文章标题:[转载]oracle 同时更新多表,在Oracle数据库中同时更新两张表的简单方法

更新时间:2023-09-10
[转载]oracle 同时更新多表,在Oracle数据库中同时更新两张表的简单方法
文章标题:[转载][Unity] 包括场景互动与射击要素的俯视角闯关游戏Demo

更新时间:2024-03-11
[转载][Unity] 包括场景互动与射击要素的俯视角闯关游戏Demo
文章标题:[转载]程序员也分三六九等?等级差异,一个看不起一个!

更新时间:2024-05-10
[转载]程序员也分三六九等?等级差异,一个看不起一个!
文章标题:[转载]海贼王 动漫 全集目录 分章节 精彩打斗剧集

更新时间:2024-01-12
[转载]海贼王 动漫 全集目录 分章节 精彩打斗剧集
名词解释
作为当前文章的名词解释,仅对当前文章有效。
回文字符串在计算机科学和数学中,回文字符串是指一个字符串,从前往后读和从后往前读完全一样。例如,在文章中的“aba”和“abccba”就是回文字符串,它们正序和逆序都相同。在该编程题中,目标是通过插入字符'a'来使得给定的字符串不再是回文字符串。
动态构造非回文字符串算法这是一种程序设计策略或算法,用于处理字符串数据,特别是当任务要求在保持字符串原有部分不变的基础上,通过插入特定字符(如本题中的字符'a')以改变其原本的回文属性,使其变为非回文字符串。算法通常会涉及遍历、判断以及可能的修改操作。
ACM国际大学生程序设计竞赛(ACM-ICPC)ACM-ICPC是一项全球范围内的高水平大学生计算机编程竞赛,由美国计算机协会(Association for Computing Machinery, ACM)主办。该竞赛旨在展示并提升大学生在算法分析、问题解决、编程技巧及团队合作等方面的能力。在文章中提到,此类竞赛经常出现与回文串相关的题目,参赛者需灵活运用算法知识来解决实际问题。
DNA序列分析在生物学领域,DNA序列分析是一种研究方法,通过对生物体DNA分子进行测序和比对,了解基因组结构、功能及进化信息等。文中提及,回文结构在DNA序列中扮演着重要角色,往往与基因调控区域相关联,因此理解回文串特性对于遗传学研究具有重要意义。
加密算法在密码学领域,加密算法是一种将明文信息转化为密文以确保信息安全传输和存储的方法。文中虽然没有直接介绍加密算法,但指出特定类型的回文串可以应用于构建加密算法的关键部分,说明回文串在高级密码学应用中具有一定价值。
延伸阅读
作为当前文章的延伸阅读,仅对当前文章有效。
在探讨了如何通过插入字符 'a' 来构造非回文字符串这一编程问题后,我们可以进一步了解字符串处理与算法优化的最新研究进展。近日,《自然》杂志子刊《自然-通讯》发表了一篇关于“在线字符串编辑与动态回文判定”的研究报告。研究者提出了一种新颖的在线算法,能够在字符串实时更新过程中高效地判断其是否为回文,并能快速找到使字符串变为非回文所需的最少编辑操作。这一成果不仅对于文本处理、数据压缩等领域具有重要价值,也对解决类似的编程挑战提供了新的思路。
此外,在ACM国际大学生程序设计竞赛(ACM-ICPC)和谷歌代码 Jam 等全球顶级编程赛事中,频繁出现与回文串相关的题目,参赛者需灵活运用算法知识来解决实际问题。比如,有题目要求选手在最短时间内编写程序,找出将一个字符串转换为非回文串的最小操作次数,这与我们讨论的文章主题不谋而合,展现了理论与实践相结合的重要性。
同时,回文串在密码学、遗传学以及文学创作等多个领域均有应用。例如,在DNA序列分析中,回文结构往往关联着基因调控的重要区域;在密码学中,特定类型的回文串可用于构建加密算法的关键部分。深入理解并熟练掌握回文串的相关性质及处理方法,无疑有助于我们在这些领域取得更多的技术突破。
总之,从基础的编程题出发,我们可以洞察到字符串处理与算法优化在前沿科研和实际应用中的深远影响。通过持续关注和学习此类问题的最新研究成果与应用案例,我们能够不断提升自身的算法设计和问题解决能力。
知识学习
实践的时候请根据实际情况谨慎操作。
随机学习一条linux命令:
pgrep -f pattern - 根据进程的完整命令行字符串查找进程ID。
随便看看
拉到页底了吧,随便看看还有哪些文章你可能感兴趣。
纯js实用T恤衫花纹图案预览特效 01-26 基于Bootstrap仿Github样式下拉列表框插件 08-08 jQuery电子邮件地址填写自动完成插件 04-30 Superset 数据源连接配置:精细化自定义SQLAlchemy URI实现数据分析与可视化,含SSL加密连接实例 03-19 jquery可任意拖动排序的导航图片效果 02-23 侧边栏个人图文简历HTML模板 12-09 Beego框架升级中的Bee工具版本兼容性问题与迁移策略:结构变更、功能接口变动及社区解决方案 12-07 Kibana无法启动:针对服务器内部错误的Elasticsearch连接、配置文件、端口冲突与资源排查解决(注:由于字数限制,未能完全包含所有关键词,但包含了核心问题描述及几个关键排查点) 11-01 ClickHouse外部表使用中文件权限与不存在问题的解决方案:错误提示、查询操作与文件路径管理实务 09-29 本次刷新还10个文章未展示,点击 更多查看。
Apache Atlas UI无法正常加载与样式丢失问题排查及解决方案:关注网络连接、浏览器缓存与开发者工具应用 09-25 Greenplum数据库中数据插入操作详解:单行多行插入与gpfdist实现大批量导入 08-02 [转载]html5 footer header,html-5 --html5教程article、footer、header、nav、section使用 07-16 [转载][GCC for C]编译选项---IDE掩盖下的天空 06-29 简洁大方珠宝钻石收藏网站模板下载 06-20 黑色高端精致汽车4s店美容html5模板下载 06-01 蓝色互联网项目融资管理平台网站模板 05-16 响应式游戏开发类企业前端cms模板下载 05-02 Beego框架动态路由实现:重定向与命令行参数驱动的路由设计实践 04-05 .NET 中字典操作避免 KeyNotFoundException:TryGetValue、ContainsKey 与 GetOrAdd 实践详解 04-04 [转载]2021/4/23爬虫第五次课(爬虫网络请求模块下下) 03-01
时光飞逝
"流光容易把人抛,红了樱桃,绿了芭蕉。"