目录
1.Problem
You've got a string SS consisting of nn lowercase English letters from your friend. It turned out that this is a number written in poman numerals. The poman numeral system is long forgotten. All that's left is the algorithm to transform number from poman numerals to the numeral system familiar to us. Characters of SS are numbered from 11 to nn from left to right. Let's denote the value of SS as f(S)f(S), it is defined as follows:
- If |S|>1|S|>1, an arbitrary integer mm (1≤m<|S|1≤m<|S|) is chosen, and it is defined that f(S)=−f(S[1,m])+f(S[m+1,|S|])f(S)=−f(S[1,m])+f(S[m+1,|S|]), where S[l,r]S[l,r] denotes the substring of SS from the ll-th to the rr-th position, inclusively.
- Otherwise S=cS=c, where cc is some English letter. Then f(S)=2pos(c)f(S)=2pos(c), where pos(c)pos(c) is the position of letter cc in the alphabet (pos(pos(a)=0)=0, pos(pos(z)=25)=25).
Note that mm is chosen independently on each step.
Your friend thinks it is possible to get f(S)=Tf(S)=T by choosing the right mm on every step. Is he right?
2.Input
The first line contains two integers nn and TT (2≤n≤1052≤n≤105, −1015≤T≤1015−1015≤T≤1015).
The second line contains a string SS consisting of nn lowercase English letters.
3.Output
Print "Yes" if it is possible to get the desired value. Otherwise, print "No".
You can print each letter in any case (upper or lower).
4.Examples
4.1input
2 -1
ba
4.2output
Yes
5.Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, t;
cin >> n >> t;
string s;
cin >> s;
t -= 1 << (s[n] - 'a');
t += 1 << (s[n - 1] - 'a');
vector<int> cnt(26, 0);
for (int i = 1; i <= n - 2; i++)
cnt[s[i] - 'a']++;
for (int i = 0; i <= 25; i++)
t += 1ll * cnt[i] * (1 << i);
if (t < 0 || t & 1)
cout << "No" << endl;
else {
t /= 2;
for (int i = 0; i <= 60; i++) {
if (t & 1) {
if (cnt[i] == 0) {
cout << "No" << endl;
return 0;
}
cnt[i]--;
}
cnt[i + 1] += cnt[i] / 2;
t /= 2;
}
cout << "Yes" << endl;
}
return 0;
}
6.Conclusion
这段代码的功能是判断给定的字符串是否可以通过改变其中的某些字符,使得最终的二进制表示中只有一个位是置位的。
具体步骤如下:1.从标准输入读入两个整数 n 和 t,以及一个长度为 n 的字符串 s。
2.对 t 进行一系列操作,减去 s[n] 对应的二进制位的值,加上 s[n-1] 对应的二进制位的值。
3.统计字符串 s 中除了首尾两个字符之外的每个字符的出现次数,保存在数组 cnt 中。
4.根据每个字符的出现次数和对应的二进制位值,更新 t 的值。
5.检查更新后的 t 是否小于 0 或者 t 是否为奇数,如果是则输出 "No" 并结束程序。
6.将 t 除以 2,并根据 t 的二进制表示逐位进行处理,确保每一位只有一个置位。如果出现无法满足的情况,输出 "No" 并结束程序,否则输出 "Yes"。总体来说,这段代码通过一系列的位运算和条件判断,判断了一种特定情况下的字符串是否符合题目的要求。