codeforces 1367D(思维)
题意描述
Polycarp wrote on the board a string s containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.
After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information.
Suppose that the string t has length m and the characters are numbered from left to right from 1 to m. You are given a sequence of m integers: b1,b2,…,bm, where bi is the sum of the distances |i−j| from the index i to all such indices j that tj>ti (consider that ‘a’<‘b’<…<‘z’). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values |i−j|.
For example, if t = “abzb”, then:
since t1=‘a’, all other indices contain letters which are later in the alphabet, that is: b1=|1−2|+|1−3|+|1−4|=1+2+3=6; since t2=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b2=|2−3|=1; since t3=‘z’, then there are no indexes j such that tj>ti, thus b3=0; since t4=‘b’, only the index j=3 contains the letter, which is later in the alphabet, that is: b4=|4−3|=1. Thus, if t = “abzb”, then b=[6,1,0,1].
Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously:
t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order; the array, constructed from the string t according to the rules above, equals to the array b specified in the input data.
思路
可以发现,如果某一位的数字为0,则说明该位所对应的字母是最大的。找到最大的字母后,再计算该字母对于所有其他字母的贡献,接着一定又用某位数字为0,重复该过程即可。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define ins(x) inserter(x,x.begin())
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=4*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int cnt[26],b[55];
bool vis[55];
void solve(){
mst(vis,0);
mst(cnt,0);
string s;cin>>s;
string ans=s;
int n;cin>>n;
vector<int> v;
rep(i,0,n) cin>>b[i];
rep(i,0,s.size()) cnt[s[i]-'a']++;
int m=n;
int now=25;
while(m>0){
int tot=0;
vector<int> pos;
//统计贡献,并且统计和该贡献相同的字母
rep(i,0,n){
if(vis[i]) continue;
int sum=0;
rep(j,0,v.size()) sum+=abs(v[j]-i);
if(sum==b[i]) pos.PB(i);
}
//方便下次循环贡献的计算
rep(i,0,pos.size()){
vis[pos[i]]=true;
tot++;
v.PB(pos[i]);
}
//找到和tot出现次数相同的最大字母
while(cnt[now]<tot) now--;
m-=tot;
rep(i,0,pos.size()){
ans[pos[i]]=(char)(now+'a');
}
now--;
}
rep(i,0,n) cout<<ans[i];
cout<<endl;
}
int main(){
IOS;
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- php DES加密算法实例分析
- php提供实现反射的方法和实例代码
- PHP实现批量修改文件名的方法示例
- Linux VPS快速下载Bilibili视频脚本 ,支持1080P/720P/360P等格式
- PHP递归统计系统中代码行数
- PHP切割整数工具类似微信红包金额分配的思路详解
- php写入文件不覆盖的实例讲解
- php解决crontab定时任务不能写入文件问题的方法分析
- Laravel项目中timeAgo字段语言转换的改善方法示例
- php生成微信红包数组的方法
- 解决php写入数据库乱码的问题
- php写入txt乱码的解决方法
- PHP实现的AES 128位加密算法示例
- php写入mysql中文乱码的实例解决方法
- php实现的支付宝网页支付功能示例【基于TP5框架】