Codeforces Round 855 (Div. 3)
复杂度 O ( n ) 复杂度O(n) 复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , t;
string s;
bool judge(string s){
string now;
for(int i = 0 ; i < n ; i ++) if(i == 0 || s[i] != s[i - 1]) now += s[i];
return now == "meow";
}
signed main(){
IOS
cin >> t;
while(t --) {
cin >> n >> s;
for(int i = 0 ; i < n ; i ++) s[i] = tolower(s[i]);
if(judge(s)) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
复杂度 O ( n ) 复杂度O(n) 复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int cnt1[30] , cnt2[30];
int t , n , k;
string s;
signed main(){
IOS
cin >> t;
while(t --){
cin >> n >> k;
cin >> s;
for(int i = 0 ; i < n ; i ++){
if(s[i] >= 'a' && s[i] <= 'z') cnt1[s[i] - 'a' + 1] += 1;
else cnt2[s[i] - 'A' + 1] += 1;
}
int res = 0;
for(int i = 1 ; i <= 26 ; i ++){
int now = min(cnt1[i] , cnt2[i]);
res += now;
cnt1[i] -= now;
cnt2[i] -= now;
}
for(int i = 1 ; i <= 26 ; i ++){
int now = cnt1[i] / 2;
if(now <= k){
k -= now;
res += now;
} else {
res += k;
k = 0;
break;
}
}
for(int i = 1 ; i <= 26 ; i ++){
int now = cnt2[i] / 2;
if(now <= k){
k -= now;
res += now;
} else {
res += k;
k = 0;
break;
}
}
for(int i = 1 ; i <= 26 ; i ++) cnt1[i] = cnt2[i] = 0;
cout << res << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
复杂度 O ( n l o g n ) 复杂度O(nlogn) 复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n , now;
signed main(){
IOS
cin >> t;
while(t --){
cin >> n;
priority_queue<int , vector<int> , less<int>> q;
int res = 0;
for(int i = 1 ; i <= n ; i ++){
cin >> now;
if(now) {
q.push(now);
} else {
if(q.size()){
res += q.top();
q.pop();
}
}
}
cout << res << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
复杂度 O ( n ) 复杂度O(n) 复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n;
string s;
signed main(){
IOS
cin >> t;
while(t --){
cin >> n >> s;
int res = n - 1;
for(int i = 0 ; i < n - 2 ; i ++){
if(s[i] == s[i + 2]) res -= 1;
}
cout << res << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
复杂度 O ( n l o g n ) 复杂度O(nlogn) 复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
/*
并查集
*/
int t , n , k ;
string st , ed;
int fa[N];
int find(int x){
if(x == fa[x]) return x;
else return fa[x] = find(fa[x]);
}
void unionn(int x , int y){
int xx = find(x);
int yy = find(y);
fa[xx] = yy;
}
signed main(){
IOS
cin >> t;
while(t --){
cin >> n >> k;
cin >> st >> ed;
st = '#' + st;
ed = '#' + ed;
for(int i = 1 ; i <= n ; i ++) fa[i] = i;
for(int i = 1 ; i <= n ; i ++){
int x = i + k;
int y = i + k + 1;
if(x <= n) unionn(i , x);
if(y <= n) unionn(i , y);
}
multiset<char>st1[n + 10] , st2[n + 10];
set<int>all;
for(int i = 1 ; i <= n ; i ++) {
int now = find(i);
all.insert(now);
st1[now].insert(st[i]);
st2[now].insert(ed[i]);
}
bool tag = 0;
for(auto x : all){
if(st1[x] != st2[x]) tag = 1;
}
if(!tag) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
复杂度 O ( 26 ∗ n ∗ l o g n ) 复杂度O(26*n*logn) 复杂度O(26∗n∗logn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , m;
unordered_map<int , int>mp[30];
int cnt[N][30] , pre[N] , nex[N];
string s;
int res;
signed main(){
IOS
cin >> n;
for(int i = 1 ; i <= n ; i ++) {
cin >> s;
m = s.size();
for(int j = 0 ; j < m ; j ++) cnt[i][s[j] - 'a' + 1] += 1;
for(int j = 0 ; j < 26 ; j ++) {
pre[i] += (cnt[i][j + 1] % 2) * (1 << j);
nex[i] += (1 - cnt[i][j + 1] % 2) * (1 << j);
}
}
// a - z
for(int i = 1 ; i <= n ; i ++){
for(int j = 0 ; j < 26 ; j ++){
if(cnt[i][j + 1]) continue;
int k = nex[i] - (1 << j);
if(mp[j + 1].find(k) != mp[j + 1].end()) res += mp[j + 1][k];
mp[j + 1][pre[i]] += 1;
}
}
cout << res << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);