传送门

题意:给你一个原串和模式串,问你能否通过两种操作把原串变成模式串。
操作方法:
1.交换任意相邻字符。
2.将k长度的相同字符子串全+1。

思路:

对于操作1,相当于我们可以任意排序原串。

结合操作2,我们可以尽可能的先将原串中相同的字符聚集起来,然后看看模式串中对应字符的数量,如果原串中更多一点,我们就把多出来的全部丢给下一位字符(+1)。

又因为要k个k个地+1,所以我们看多出来的那些,不够凑到k个的,先用变量bios记录下来,说明我们要用到后面的字符和当前这些剩下的字符一起+1。

如果原串字符更少一些,那么前面的bios就起作用了,就在此时和前面说的剩余字符一起+1,同时更新bios。到最后判断一下bios是否等于0且字符个数全部相等即可。

详见代码注释。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[500];
ll b[500];
string s;
string t; bool check(ll k)
{
mem(a,0); mem(b,0);
for(int i=0; i<s.size(); i++) a[s[i]]++; //记录两串字符个数
for(int i=0; i<t.size(); i++) b[t[i]]++;
int bios = 0; //偏移量
for(int i='a'; i<'z'; i++) //比较两串的每个字符,每一步都要变成相等(因为只能从小到大变化,所以当前变不到相等以后就变不到了)
{
if(a[i] > b[i]) // 当前字符原串更多的话
{
int d = a[i] - b[i]; // 看看差多少
a[i] -= d; //全部减去
a[i+1] += d; //丢给下一位
bios += d - (d/k)*k ; //看看有多少是凑不到k个的(剩下来的),说明要利用到后面的字符一起+1
}
else // 如果原串少了
{
int d = b[i] - a[i]; //看看少了多少
if(bios >= d) bios -= d; //能用bios弥补回来就问题不大
else return false; //否则直接false
}
}
if(bios||a['z'] != b['z']) return false; //比较判断
return true;
} int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), k = read();
cin>>s;
cin>>t;
puts(check(k)?"YES":"NO");
}
return 0;
}

Codeforces Round #685 (Div. 2) C. String Equality 思维的更多相关文章

  1. Codeforces Round #402 (Div. 2) D. String Game

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  2. Codeforces Round #402 (Div. 2) D. String Game(二分答案水题)

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  3. Codeforces Round #402 (Div. 2) D String Game —— 二分法

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  4. 【二分答案】Codeforces Round #402 (Div. 2) D. String Game

    二分要删除几个,然后暴力判定. #include<cstdio> #include<cstring> using namespace std; int a[200010],n, ...

  5. Codeforces Round #685 (Div. 2)

    待补 A #include <bits/stdc++.h> using namespace std; int n; int main() { int __; scanf("%d& ...

  6. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  7. Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)

    https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...

  8. Codeforces Round #380 (Div. 2)/729D Sea Battle 思维题

    Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the ...

  9. Codeforces Round #459 (Div. 2) C题【思维好题--括号匹配问题】

    题意:给出一个串,只包含 ( ? ) 三种符号,求出有多少个子串是完美匹配的. ( ) ? ) => ( ) ( ) 完美匹配( ( ) ? => ( ( ) )完美匹配? ? ? ? = ...

  10. Codeforces Round #646 (Div. 2) B. Subsequence Hate (思维,前缀和)

    题意:给你一个只含有\(0\)和\(1\)的字符串,每次操作可以将\(0\)改成\(1\)或\(1\)改成\(0\),问最少操作多少次,使得子序列中不含有\(010\)和\(101\). 题解:仔细想 ...

随机推荐

  1. 🎀idea获取当前项目git仓库地址

    简介 在idea中快速获取当前项目的远程仓库地址 方案一 右键项目 选择Git 选择Manage Remotes 弹框中的URL就是远程仓库地址 方案二 打开terminal 命令行 直接Git命令查 ...

  2. 1678. 设计 Goal 解析器

    1678. 设计 Goal 解析器 class Solution { public String interpret(String command) { char[] ch = command.toC ...

  3. nodejs使用sequelize vscode报错:Type 'Model<any, any, any>' is not a constructor function type.的解决办法

    我的模型定义如下: import { Model, DataTypes } from "sequelize"; // 定义资源模型 class Rule extends Model ...

  4. 2025dsfz集训Day4:BFS及其优化

    DAY4: BFS及其优化 \[Designed\ By\ FrankWkd\ -\ Luogu@Lwj54joy,uid=845400 \] 特别感谢 此次课的主讲 - Kwling BFS 广度优 ...

  5. OneNote Embedded 文件滥用检测

    本文分享自天翼云开发者社区<OneNote Embedded 文件滥用检测>,作者:Icecream 攻击技术 在这些网络钓鱼活动中被滥用的OneNote功能是在图片后面隐藏嵌入式文件,诱 ...

  6. 69.9K star!这个API调试神器让你告别Postman,开源免费真香!

    嗨,大家好,我是小华同学,关注我们获得"最新.最全.最优质"开源项目和高效工作学习方法 Hoppscotch 是一款专为开发者打造的轻量级API调试工具,凭借其极简的界面设计和强大 ...

  7. 【工具】Vscode翻译插件推荐(不用谷歌翻译api、支持短句英汉互译、支持查词、支持自动补全、不需要浏览器)

    2024/04/24说明:这篇暂时修改为粉丝可见,因为正在冲粉丝量,等到我弄完了粉丝量的要求,我就改回来!不方便看到全文的小伙伴不好意思!! 需求: 1)偶尔需要查英文生词: 2)有时候想不起来中文对 ...

  8. RPC实战与核心原理之流量回放

    流量回放:保障业务技术升级的神器 回顾 时钟轮在 RPC 中的应用,核心原理就一个关键字"分而治之",我们可以把它用在任何需要高效处理大量定时任务的场景中,最具有代表性的就是在高并 ...

  9. 稀疏贝叶斯谱估计及EM算法求解

    稀疏贝叶斯 稀疏贝叶斯学习(sparse bayes learning,SBL)最早被提出是作为一种机器学习算法[1].但是在这里我们主要用它来做谱估计,作为求解稀疏重构问题的方法[2].稀疏重构还有 ...

  10. Tomcat版本匹配问题

    官方链接http://tomcat.apache.org/whichversion.html Servlet Spec JSP Spec EL Spec WebSocket Spec JASPIC S ...