解码字母到整数映射
2023-02-18 16:34:53 时间
给你一个字符串 s,它由数字('0' - '9')和 '#' 组成。我们希望按下述规则将 s 映射为一些小写英文字符:
字符('a' - 'i')分别用('1' - '9')表示。
字符('j' - 'z')分别用('10#' - '26#')表示。
返回映射之后形成的新字符串。
题目数据保证映射始终唯一。
示例 1:
输入:s = "10#11#12"
输出:"jkab"
解释:"j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
示例 2:
输入:s = "1326#"
输出:"acz"
示例 3:
输入:s = "25#"
输出:"y"
示例 4:
输入:s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
输出:"abcdefghijklmnopqrstuvwxyz"
代码:
#include<iostream>
// #include <algorithm>
#include<bits/stdc++.h>
using namespace std;
string freqAlphabets(string s){
string str;
int len = s.length();
//倒过来处理简单,不建议顺序进行,否则要多次判断
for (int i = len-1; i >=0;)
{
if (s[i]=='#')
{
int value = (s[i-2]-'0')*10+(s[i-1]-'0');
str+=(value+'a'-1);
//减3 要算上'#','#'前面两个字符已经用过了,所以减3
i=i-3;
}else{
int value = (s[i]-'0');
str.push_back(value+'a'-1);
i=i-1;
}
}
// #include <algorithm>
reverse(str.begin(),str.end());
return str;
}
int main(){
string s;
cin>>s;
cout<<freqAlphabets(s)<<endl;
}
相关文章
- 论文解读《Bilinear Graph Neural Network with Neighbor Interactions》
- 论文解读《Deep Attention-guided Graph Clustering with Dual Self-supervision》
- 论文解读(ClusterSCL)《ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs》
- 论文解读(SimGRACE)《SimGRACE: A Simple Framework for Graph Contrastive Learning without Data Augmentation》
- 论文解读(GTN)《Graph Transformer Networks》
- 论文解读(SAGPool)《Self-Attention Graph Pooling》
- 论文解读(DiffPool)《Hierarchical Graph Representation Learning with Differentiable Pooling》
- 论文解读《Deeper Insights into Graph Convolutional Networks for Semi-Supervised Learning》
- 论文解读(GMT)《Accurate Learning of Graph Representations with Graph Multiset Pooling》
- 论文解读《Measuring and Relieving the Over-smoothing Problem for Graph NeuralNetworks from the Topological View》
- 论文解读(DAGNN)《Towards Deeper Graph Neural Networks》
- 论文解读(Debiased)《Debiased Contrastive Learning》
- 论文解读(IGSD)《Iterative Graph Self-Distillation》
- 论文解读(SUBG-CON)《Sub-graph Contrast for Scalable Self-Supervised Graph Representation Learning》
- 论文解读(MERIT)《Multi-Scale Contrastive Siamese Networks for Self-Supervised Graph Representation Learning》
- 论文解读(MCNS)《Understanding Negative Sampling in Graph RepresentationLearning》
- 论文解读(GROC)《Towards Robust Graph Contrastive Learning》
- 图神经网络的攻击防御
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- 论文解读(DCRN)《Deep Graph Clustering via Dual Correlation Reduction》