首页 > 其他分享 >CF Round946 (Div. 3)B:如何写映射

CF Round946 (Div. 3)B:如何写映射

时间:2024-05-22 09:21:04浏览次数:14  
标签:string 映射 encoding int Round946 character CF replaced Div

Symmetric Encoding

题目描述

Polycarp has a string $ s $ , which consists of lowercase Latin letters. He encodes this string using the following algorithm:

  • first, he constructs a new auxiliary string $ r $ , which consists of all distinct letters of the string $ s $ , written in alphabetical order;
  • then the encoding happens as follows: each character in the string $ s $ is replaced by its symmetric character from the string $ r $ (the first character of the string $ r $ will be replaced by the last, the second by the second from the end, and so on).

For example, encoding the string $ s $ ="codeforces" happens as follows:

  • the string $ r $ is obtained as "cdefors";
  • the first character $ s_1 $ ='c' is replaced by 's';
  • the second character $ s_2 $ ='o' is replaced by 'e';
  • the third character $ s_3 $ ='d' is replaced by 'r';
  • ...
  • the last character $ s_{10} $ ='s' is replaced by 'c'.

The string $ r $ and replacements for $ s $ ="codeforces".Thus, the result of encoding the string $ s $ ="codeforces" is the string "serofedsoc".

Write a program that performs decoding — that is, restores the original string $ s $ from the encoding result.

输入格式

The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of the string $ b $ .

The second line of each test case contains a string $ b $ of length $ n $ , consisting of lowercase Latin letters — the result of encoding the original string $ s $ .

It is guaranteed that the sum of the values of $ n $ over all test cases in the test does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output the string $ s $ from which the encoding result $ b $ was obtained.

样例 #1

样例输入 #1

5
10
serofedsoc
3
ttf
9
tlrhgmaoi
1
w
15
hnndledmnhlttin

样例输出 #1

codeforces
fft
algorithm
w
meetinthemiddle

本题思路

写映射

AC_code

双指针 写映射

#include <bits/stdc++.h>

using namespace std;

int _, n;

void solve() {
    string s, r;
    cin >> s;
    for(int i = 0; i < n; ++ i) {
        if(count(r.begin(), r.end(), s[i])) continue;//count返回出现次数
        r += s[i];
    }
    sort(r.begin(), r.end());
    vector<char> mp(26);
    for(int i = 0, j = r.size() - 1; i <= j;  ++ i, -- j) {//char数组双指针转换
        mp[r[i] - 'a'] = r[j];
        mp[r[j] - 'a'] = r[i];
    }
    for(auto it : s) cout << mp[it - 'a'];
    cout << endl;
}

int main()
{
    cin >> _;
    while(_ --) {
        cin >> n;
        solve();
    }
    return 0;
}

单指针变化+嵌套

#include <bits/stdc++.h>

using namespace std;

const int N = 2 * 1e5 + 10;

char s[N], t[N], mp[N];
int _, n;

void solve() {
    int vis[26] = {0};//局部变量节省多组输入初始化
    int m = 0;
    for(int i = 1; i <= n; ++ i) {
        if(vis[s[i] - 'a'] == 0)//t数组存出现过的值
            t[++ m] = s[i], vis[s[i] - 'a'] = 1;
    }
    sort(t + 1, t + m + 1);//排序
    //此时已经知道一一对应的关系,映射
    for(int i = 1; i <= m; ++ i) mp[t[i]] = t[m - i + 1];
    for(int i = 1; i <= n; ++ i) {
        s[i] = mp[s[i]];//转换
    }
    puts(s + 1);    
}

int main()
{
    cin >> _;
    while(_ --) {
        cin >> n >> s + 1;
        solve();   
    }
    return 0;
}

get到技能:如何映射并转换

1.指针变换写映射 + 嵌套

for(int i = 1; i <= m; ++ i) mp[t[i]] = t[m - i + 1];
    for(int i = 1; i <= n; ++ i) {
        s[i] = mp[s[i]];//转换
    }

2.双指针扫描 + 值映射


vector<char> mp(26);
for(int i = 0, j = r.size() - 1; i <= j;  ++ i, -- j) {
    mp[r[i] - 'a'] = r[j];
    mp[r[j] - 'a'] = r[i];
}
for(auto it : s) cout << mp[it - 'a'];

总结

取出字符串r没什么难度,赛场脑子掉线写映射被卡(在写用整形+'a'转回字符映射),然后爆炸了,忽略了直接存一下的可能....

for(int i = 0; i < n; ++ i) {//当时写了一个0~n-1的转换,看起来没什么问题
    cout << s[i] << ' ' << s[n - i - 1] << endl;
}

标签:string,映射,encoding,int,Round946,character,CF,replaced,Div
From: https://www.cnblogs.com/OVSolitario-io/p/18205459

相关文章

  • CF1278F Cards(斯特林数+二项式定理)
    题意简述有\(m\)张牌,其中有一张是王牌。你现在可以洗\(n\)次牌(每种牌堆序列等概率出现),然后查看牌堆顶的第一张牌。设\(x\)为\(n\)次洗牌中第一张牌是王牌的次数,则得分为\(x^k\)。求得分的期望值对\(998244353\)取模的值。\(1\len,m<998244353,k\le5000\)分析将......
  • CF1515F Phoenix and Earthquake
    CF1515FPhoenixandEarthquake证明题。思路考虑不合法的情况,如果\(\suma_i<(n-1)\timesx\),肯定是不合法的。再考虑对于一个可行的情况,最后缩的边肯定形成一棵树,所以我们大胆假设:任意一棵生成树只要满足\(\suma_i\geq(n-1)\timesx\)有合法的缩边方案。考虑归纳证......
  • CF Div. 3 C Beautiful Triple Pairs
    原题链接标签组合数学(combinatorics)数据结构(datastructures)题目大意一个数组\(a\)。对于每个\(j\)(\(1\lej\len-2\))写出一个由\([a_j,a_{j+1},a_{j+2}]\)组成的三元组。满足以下条件之一,那么这对三元组就是美丽的:\(b_1\nec_1\)和\(b_2=c_2\)......
  • WCF服务的各种绑定
    绑定配置元素说明BasicHttpBindingbasicHttpBinding>一个绑定,适用于与符合WS-BasicProfile的Web服务(例如基于ASP.NETWeb服务(ASMX)的服务)进行的通信。此绑定使用HTTP作为传输协议,并使用文本/XML作为默认的消息编码。WSHttpBindingwsHttpBinding>一个安......
  • Codeforces Round 946 (Div. 3)
    CodeforcesRound946(Div.3)总结:赛时状态很好,做出了感觉平常会赛时寄掉但是赛后补题的E,但是也因此花费时间太多,没时间去做更简单的F和G,赛后G用时十分钟AC,F码的有点麻烦,用时40分钟左右,感觉三个小时能AK?A.PhoneDesktop题意:给定3*5的平面,有a个1*1的格子和b个2*2的格子,求完全......
  • Kmesh进入CNCF云原生全景图,实现网格治理sidecarless化
    本文分享自华为云社区《Kmesh进入CNCF云原生全景图》 ,作者:云容器大未来。近日,Kmesh 正式进入CNCF云原生全景图,位于ServiceMesh 类别下。CNCFLandscape在云原生实践过程中的每个环节帮助用户了解有哪些具体的软件和产品选择,Kmesh进入CNCFLandscape,成为了CNCF构建云......
  • 「杂题乱刷」CF1973D
    链接算简单题。你发现最大值肯定可以用\(n\)次查出来。然后可以证明\(ans\le\frac{n}{k}\)。总次数为\(n+\frac{n}{k}\timesk\le2n\)。代码:点击查看代码/*Tips:你数组开小了吗?你MLE了吗?你觉得是贪心,是不是该想想dp?一个小时没调出来,是不是该考虑换题?打c......
  • CF1618FG
    闲的想做道*2000,于是找到了CF1618F。wc,我怎么会了。o,只有绿啊,没事了。再看看G吧。wc,我怎么又会了???wc,这有蓝???1618F首先,我们发现对于一个合法二进制数添加\(0\),相当于去除末尾所有的\(0\)后再翻转,即翻转后会变成\(1\ldots1\)的形式。引理:二进制数如果在非第一次操......
  • vue3+ts 指令拖拽div案例
    <template><divclass="box"v-move><divclass="header"></div><div>内容</div></div></template><scriptsetuplang="ts">import{ref,Directive,DirectiveBinding}......
  • Codeforces Round 945 (Div. 2) (A - E)
    A每一轮对总分的贡献都是\(2\),如果\(p_1+p_2+p_3\)为奇数则无解。\(p_1+p_2\lep_3\),最多\(p_1+p_2\)轮。\(p_1+p_2>p_3\),可以\(1,2\)轮流将\(3\)耗完,然后互相匹配,最多\(\dfrac{p_1+p_2+p_3}{2}\)。B如何判断一个\(k_0\)是否符合条件?处......