今天我们聊聊 Coderforces 字符串水题合集。
字符串就是string。
这是string的百度翻译,我们要谈的是画框的。↓
string是C++、java、VB等编程语言中的字符串,字符串是一个特殊的对象,属于引用类型。 在java、C#中,String类对象创建后,字符串一旦初始化就不能更改,因为string类中所有字符串都是常量,数据是无法更改,由于string对象的不可变,所以可以共享。对String类的任何改变,都是返回一个新的String类对象。 C++标准库中string类以类型的形式对字符串进行封装,且包含了字符序列的处理操作。【 本词条由“科普中国”科学百科词条编写与应用工作项目 审核 】
Codeforces是一家为计算机编程爱好者提供在线评测系统的俄罗斯网站。该网站由萨拉托夫国立大学的一个团体创立并负责运营。
不管学字符(char)还是字符串(string),都要学ASCLL码,如下图:
那好,给你做几道判断题:
1.Codeforces简称cf,是美国主办的。( )
2.string在编程中是弦乐器的意思。 ( )
3.水题指的是狂简单的题。 ( )
参考答案:1.✘ 2.✘ 3.✔
目录
96A - Football
837A - Text Volume
118A - String Task
1105B - Zuhair and Strings
133A - HQ9+
1674B - Dictionary
236A - Boy or Girl
745A - Hongcow Learns the Cyclic Shift
题目描述:
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Petya loves football very much. One day, as he was watching a football match, he was writing the players’ current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.
Input
The first input line contains a non-empty string consisting of characters “0” and “1”, which represents players. The length of the string does not exceed 100 characters. There’s at least one player from each team present on the field.
Output
Print “YES” if the situation is dangerous. Otherwise, print “NO”.
Examples
input
001001
output
NO
input
1000000001
output
YES
题目大意:
题意很简单,及输入一个字符串,输出是否有连续7位都是0或都是1。
题目思路:
从0循环到s.size()-7,依次用substr函数取子串。
substr函数在oracle中使用表示被截取的字符串或字符串表达式。和instr()函数不同,instr()函数是要截取的字符串在源字符串中的“位置”,substr()函数是截取字符串的“内容”。
子串,计算机术语,串中任意个连续的字符组成的子序列称为该串的子串。
AC代码:
#include
using namespace std;
int main(){
string s; cin>>s;
for(int i=0;i<=int(s.size()-7);i++){
string t=s.substr(i,7);
if(t=="0000000"||t=="1111111"){
cout<<"YES";
return 0;
}
}
cout<<"NO";
return 0;
}
题目描述:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a text of single-space separated words, consisting of small and capital Latin letters.
Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.
Calculate the volume of the given text.
Input
The first line contains one integer number n (1 ≤ n ≤ 200) — length of the text.
The second line contains text of single-space separated words s1, s2, …, si, consisting only of small and capital Latin letters.
Output
Print one integer number — volume of text.
Examples
Input
7
NonZERO
Output
5
Input
24
this is zero answer text
Output
0
Input
24
Harbour Space University
Output
1
Note
In the first example there is only one word, there are 5 capital letters in it.
In the second example all of the words contain 0 capital letters.
题目大意:
输入一个数n,在输入n个字符(包含空格),每个空格隔开两个单词,输出一个单词最多有多少个大写字母。
题目思路:
getline函数输入,注意要加getchar函数读入换行(\n),依次用max函数取最大值。
getline是C++标准库函数;但不是C标准库函数,而是POSIX(IEEE Std 1003.1-2008版本及以上)所定义的标准库函数(在POSIX IEEE Std 1003.1-2008标准出来之前,则只是GNU扩展库里的函数)。getline会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束:1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度。
getchar是读入函数的一种。它从标准输入里读取下一个字符,相当于getc(stdin)。返回类型为int型,为用户输入的ASCII码或EOF。【本词条由“科普中国”科学百科词条编写与应用工作项目 审核 。】
函数max函数用于求向量或者矩阵的最大元素,或几个指定值中的最大值。MATLAB等高级编程语言中常用有三种形式:max(A)、max(A,B)、max(A,[],dim)。
AC代码:
#include
using namespace std;
int main(){
int n,cnt=0,ans=0; cin>>n;
getchar();
string s;
getline(cin,s);
for(int i=0;i='A'&&s[i]<='Z')
cnt++;
if(s[i]==' '){
ans=max(ans,cnt);
cnt=0;
}
if(i==n-1) ans=max(ans,cnt);
}
cout<
题目描述:
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Petya started to attend programming lessons. On the first lesson his task was to write a simple program. The program was supposed to do the following: in the given string, consisting if uppercase and lowercase Latin letters, it:
deletes all the vowels,
inserts a character "." before each consonant,
replaces all uppercase consonants with corresponding lowercase ones.
Vowels are letters "A", "O", "Y", "E", "U", "I", and the rest are consonants. The program's input is exactly one string, it should return the output as a single string, resulting after the program's processing the initial string.
Help Petya cope with this easy task.
Input
The first line represents input string of Petya's program. This string only consists of uppercase and lowercase Latin letters and its length is from 1 to 100, inclusive.
Output
Print the resulting string. It is guaranteed that this string is not empty.
Sample test(s)
input
tour
output
.t.r
input
Codeforces
output
.c.d.f.r.c.s
input
aBAcAba
output
.b.c.b
题目大意:
给一个字符串,要求输出一个序列,只包含原字符串的非元音、非半元音字母(即不是 "A", "O", "Y", "E", "U", "I")的小写形式,每个字母输出前要加一个点。
题目思路:
依次循环判断,注意转换大小写。
AC代码:
#include
using namespace std;
int main(){
string c; cin>>c;
for(int i=0;i='A') c[i]+='a'-'A';
cout<<"."<
题目描述:
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Given a string s ss of length n nn and integer k ( 1 ≤ k ≤ n ) k\ (1≤k≤n)k (1≤k≤n). The string s ss has a level x xx, if x xx is largest non-negative integer, such that it’s possible to find in s ss:
x xx non-intersecting (non-overlapping) substrings of length k kk,
all characters of these x substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings).
A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers i ii and j ( 1 ≤ i ≤ j ≤ n ) j\ (1≤i≤j≤n)j (1≤i≤j≤n), denoted as s [ i … j ] s[i…j]s[i…j] = “s i s i + 1 … s j s_is_{i+1}…s_jsi si+1…sj”.
For example, if k = 2 k=2k=2, then:
the string “aabb” has level 1 11 (you can select substring “aa”),
the strings “zzzz” and “zzbzz” has level 2 22 (you can select two non-intersecting substrings “zz” in each of them),
the strings “abed” and “aca” have level 0 00 (you can’t find at least one substring of the length k = 2 k=2k=2 containing the only distinct character).
Zuhair gave you the integer k kk and the string s of length n nn. You need to find x xx, the level of the string s ss.
Input
The first line contains two integers n nn and k ( 1 ≤ k ≤ n ≤ 2 ⋅ 1 0 5 ) k\ (1≤k≤n≤2⋅10^5)k (1≤k≤n≤2⋅10
5
) — the length of the string and the value of k kk.
The second line contains the string s ss of length n consisting only of lowercase Latin letters.
Output
Print a single integer x xx — the level of the string.
Examples
input
8 2
aaacaabb
output
2
input
2 1
ab
output
1
input
4 2
abab
output
0
Note
In the first example, we can select 2 22 non-intersecting substrings consisting of letter ‘a’: “(aa)ac(aa)bb”, so the level is 2 22.
In the second example, we can select either substring “a” or “b” to get the answer 1 11.
题目大意:
给出一个长度为n的字符串,在字符串中查找连续出现k次的字母,中间不能有重叠,连续出现k次的字母最多出现了几次。
题目思路:暴力枚举。
暴力枚举精讲:算法(二)暴力枚举_block2333的博客-CSDN博客_暴力枚举法一、概述1、概念枚举,顾名思义,就是将所有情况都举出,并判断其是否符合题目条件。所以枚举的基本方法便是分析题意后,找到一个合适的维度列举每一个元素,以完成题目。其中如何找到一个合适的维度来进行枚举便是其中的最大难点。2、枚举的基本条件(1)时间条件枚举范围小或时间无限制(蓝桥杯填空题)一般来说主流的OJ当中,1000ms的时间限制下可以运行操作数为107以内的运算(通常106以内较为保险),所以在采用枚举方法之前最好看一下数据范围,确保整个程序的执行操作数不会超过106-107这个量级,如果超过https://blog.csdn.net/weixin_44788545/article/details/107576869
算法学习暴力枚举算法学习暴力枚举http://www.360doc.com/content/18/1210/15/5315_800675590.shtml 暴力枚举法总结 - 百度文库https://wenku.baidu.com/view/46d4e3001a2e453610661ed9ad51f01dc3815744.html
博主从TLE到WA到AC历经了千辛万苦。
准确来说 ,我陷入了一个循环……如下图。 (附:卡农在线听)
在2,6,26几个点之间产生了循环无限流……我成bug_man了。
AC代码:
#include
#define geta(n,m) for(int i=n;i>a[i];}
using namespace std;
int main(){
int sz,k,len=1; cin>>sz>>k;
string s; cin>>s; s+='!';
int ch[150];
for(int i=0;i<130;i++) ch[i]=0;
for(int i=0;i
题目描述:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
HQ9+ is a joke programming language which has only four one-character instructions:
"H" prints "Hello, World!",
"Q" prints the source code of the program itself,
"9" prints the lyrics of "99 Bottles of Beer" song,
"+" increments the value stored in the internal accumulator.
Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored.
You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output.
Input
The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.
Output
Output "YES", if executing the program will produce any output, and "NO" otherwise.
Sample test(s)
Input
Hi!
Output
YES
Input
Codeforces
Output
NO
Note
In the first case the program contains only one instruction — "H", which prints "Hello, World!".
In the second case none of the program characters are language instructions.
题目大意:
HQ9+是一种笑话编程语言,只有四条单字符指令:“H”印着“你好,世界!”,“Q”打印程序本身的源代码,“9”印刷了“99瓶啤酒”的歌词,“+”增加存储在内部累加器中的值。你会得到一个用HQ9+编写的程序,你必须弄清楚执行这个程序是否会产生任何输出。(大小写敏感)
题目思路:极度水,思路略。(我一分钟AC了)
很 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
AC代码:
#include
using namespace std;
int main(){
string s; cin>>s;
for(int i=0;i
这题是最近Codeforces Round #786 (Div. 3)的题。
Codeforces Round #786 (Div. 3)https://codeforces.com/contest/1674 |
---|
my结果是:点击后自己看吧。
附:比赛通知
Hello! Codeforces Round #786 (Div. 3) will start at Monday, May 2, 2022 at 22:35UTC+8. You will be offered 6 or 7 problems (or 8) with expected difficulties to compose an interesting competition for participants with ratings up to 1600. However, all of you who wish to take part and have rating 1600 or higher, can register for the round unofficially.
The round will be hosted by rules of educational rounds (extended ACM-ICPC). Thus, during the round, solutions will be judged on preliminary tests, and after the round it will be a 12-hour phase of open hacks.
You will be given 6 or 7 (or 8) problems and 2 hours to solve them.
Note that the penalty for the wrong submission in this round (and the following Div. 3 rounds) is 10 minutes.
Remember that only the trusted participants of the third division will be included in the official standings table. As it is written by link, this is a compulsory measure for combating unsporting behavior. To qualify as a trusted participants of the third division, you must:
Regardless of whether you are a trusted participant of the third division or not, if your rating is less than 1600, then the round will be rated for you.
The problems were invented and prepared by Adilbek adedalic Dalabaev, Alexander fcspartakm Frolov, Ivan BledDest Androsov and Mikhail awoo Piklyaev. Also huge thanks to Mike MikeMirzayanov Mirzayanov for great systems Polygon and Codeforces.
Also huge thanks to I_Remember_Olya_ashmelev, Vladosiya, mesanu and I.AM.THE.WILL for testing the round and valuable feedback on the problems!
Good luck!
UPD: Editorial is published!
言归正传,来看这道B题。
题目描述:
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.
The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word aa comes earlier than word bb in the dictionary if one of the following conditions hold:
So, the dictionary looks like that:
You are given a word ss from the Berland language. Your task is to find its index in the dictionary.
Input
The first line contains one integer tt (1≤t≤6501≤t≤650) — the number of test cases.
Each test case consists of one line containing ss — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).
Output
For each test case, print one integer — the index of the word ss in the dictionary.
Example
input
Copy
7 ab ac az ba bc zx zy
output
Copy
1 2 25 26 27 649 650
题目大意:伯兰语的单词由两个不同的拉丁小写字母组成。《伯兰词典》以字典序收录了这种语言的所有单词。例如:
单词1:ab
单词2:ac
...
单词25:az
单词26:ba
单词27:bc
...
单词649:zx
单词650:zy
你被赋予一个来自伯兰语的单词s。你的任务是在字典中找到它的索引。
题目思路:不解释了,直接上代码。
AC代码:
#include
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int tc; cin>>tc;
while(tc--){
string s; cin>>s;
int ans=0;
ans+=(s[0]-'a')*25;
if(s[1]
如果你问 cin.tie(0); ios::sync_with_stdio(0); 是什么东东,那我告诉你:
关于cin cout 和 scanf printf。做题的时候尽量使用scanf printf。下面告诉你一个小常识,不要惊讶:cin cout 比 scanf printf慢20倍左右!
一旦遇到大数据量,光是读入就有可能跪掉。
你或许可以使用std::ios::sync_with_stdio(false);这条语句关掉scanf 和cin 的同步加快效率。但是即使是这样cin 还要慢 5倍左右,而且一旦使用了这条语句,scanf和cin 混用可能就会造成一些奇怪的问题。
tie
tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。
sync_with_stdio
这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑到了一起。
题目描述:点击打开题目
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network.
But yesterday, he came to see "her" in the real world and found out "she" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names.
This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method.
Input
The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters.
Output
If it is a female by our hero's method, print "CHAT WITH HER!" (without the quotes), otherwise, print "IGNORE HIM!" (without the quotes).
Sample test(s)
Input
wjmzbmr
Output
CHAT WITH HER!
Input
xiaodao
Output
IGNORE HIM!
Input
sevenkplus
Output
CHAT WITH HER!
Note
For the first example. There are 6 distinct characters in "wjmzbmr". These characters are: "w", "j", "m", "z", "b", "r". So wjmzbmr is a female and you should print "CHAT WITH HER!".
题目大意:给你一个字符串s,问这个字符串有几个不同的字符,若是奇数个输出IGNORE HIM!,否则输出CHAT WITH HER!
题目思路:开个bool数组,储存字符串里的字符是否出现,再计数判断。
AC代码:
#include
using namespace std;
int main(){
cin.tie(0); ios::sync_with_stdio(0); //防超时
int g=0; bool f[130];
for(int i=0;i<=127;i++) f[i]=0; //清零
string s; cin>>s;
for(int i=0;i
点击打开题目
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.
Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word "abracadabra" Hongcow will get words "aabracadabr", "raabracadab" and so on.
Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.
Input
The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'–'z').
Output
Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.
Examples
input
Copy
abcd
output
Copy
4
input
Copy
bbb
output
Copy
1
input
Copy
yzyz
output
Copy
2
Note
For the first sample, the strings Hongcow can generate are "abcd", "dabc", "cdab", and "bcda".
For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate "bbb".
For the third sample, the two strings Hongcow can generate are "yzyz" and "zyzy".
题目大意:给定一个字符串,字符串有小写字母组成,通过将最后一个字符移动到第一个,能组成多少种不同的字符串。
题目思路:删、插、删、插……就行了。
AC代码:
#include
using namespace std;
int main(){
mapmp;
string s; cin>>s;
int cnt=0;
for(int i=0;i
好啦,本期总结就到这里,下期再见咯!没关注的大佬赶紧关注我吧!