1037A,
1037A,
A. Diverse Substring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string ss, consisting of nn lowercase Latin letters.
A substring of string ss is a continuous segment of letters from ss. For example, "defor" is a substring of "codeforces" and "fors" is not.
The length of the substring is the number of letters in it.
Let's call some string of length nn diverse if and only if there is no letter to appear strictly more than n2n2 times. For example, strings "abc" and "iltlml" are diverse and strings "aab" and "zz" are not.
Your task is to find any diverse substring of string ss or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.
Input
The first line contains a single integer nn (1≤n≤10001≤n≤1000) — the length of string ss.
The second line is the string ss, consisting of exactly nn lowercase Latin letters.
Output
Print "NO" if there is no diverse substring in the string ss.
Otherwise the first line should contain "YES". The second line should contain any diversesubstring of string ss.
Examples
input
Copy
10 codeforces
output
Copy
YES code
input
Copy
5 aaaaa
output
Copy
NO
Note
The first example has lots of correct answers.
Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to "No comments" answer.
题意:这个题意还是有点迷,昨晚花了很久时间,才ac,让我们找这个子串中有没有任何字母个数不超过n/2的子串,有就输出,没有就输出No。
题解: 要是一直跟题意较真,那就麻烦了,暴力查有没有两个相邻字符串是否一样,有不不一样的就输出这两个字母当做满足的子串,否则就是No.
c++:
#include<bits/stdc++.h>
using namespace std;
char s[1001];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s);
int flag=0;
for(int i=1;i<n;i++)
{
if(s[i]!=s[i-1])
{
puts("YES");
printf("%c%c\n",s[i-1],s[i]);
return 0;
}
}
puts("NO");
return 0;
}
python:
n=int(input());s=input()
for i in range(1,len(s)):
if s[i]!=s[i-1]:
print("YES\n"+s[i-1]+s[i])
exit()
print("NO")
相关文章
- 暂无相关文章
用户点评