当前位置:网站首页>1807. Replace the parentheses in the string
1807. Replace the parentheses in the string
2022-07-04 22:02:00 【Mr Gao】
1807. Replace the parentheses in the string
Give you a string s , It contains some pairs of brackets , Each bracket contains a Non empty Key .
For example , character string "(name)is(age)yearsold" in , Yes Two Bracket pair , Each contains keys "name" and "age" .
You know the values of many keys , These relationships are made up of two-dimensional string arrays knowledge Express , among knowledge[i] = [keyi, valuei] , The key keyi The corresponding value is valuei .
You need to replace all Pair of brackets . When you replace a pair of brackets , And the key it contains is keyi when , You need :
take keyi And parentheses with the corresponding values valuei Replace .
If from knowledge The value of a key cannot be found in , You need to keyi And brackets with question marks "?" Replace ( You don't need quotes ).
knowledge Each key in will appear at most once .s There will be no nested parentheses in .
Please return to replace all The result string after the bracket pair .
Example 1:
Input :s = “(name)is(age)yearsold”, knowledge = [[“name”,“bob”],[“age”,“two”]]
Output :“bobistwoyearsold”
explain :
key “name” The corresponding value is “bob” , So will “(name)” Replace with “bob” .
key “age” The corresponding value is “two” , So will “(age)” Replace with “two” .
Example 2:
Input :s = “hi(name)”, knowledge = [[“a”,“b”]]
Output :“hi?”
explain : Because I don't know the key “name” Corresponding value , So use “?” Replace “(name)” .
Example 3:
Input :s = “(a)(a)(a)aaa”, knowledge = [[“a”,“yes”]]
Output :“yesyesyesaaa”
explain : Same key in s There may be many times .
key “a” The corresponding value is “yes” , So put all the “(a)” Replace with “yes” .
Be careful , Not in brackets “a” It doesn't need to be replaced .
This problem is obviously relatively simple to use hash table, but , The author tried the conventional method this time , Interested can learn :
int f(char *s,int index,char ***knowledge,int knowledgeSize){
int i;
for(i=0;i<knowledgeSize;i++){
char *p=knowledge[i][0];
int j=index,k=0,r=0;
while(s[j]!='('&&p[k]!='\0'){
if(s[j++]!=p[k++]){
r=1;
break;
}
}
if(r==0&&s[j]==')'&&p[k]=='\0'){
return i;
}
}
return -1;
}
char * evaluate(char * s, char *** knowledge, int knowledgeSize, int* knowledgeColSize){
char *re=(char *)malloc(sizeof(char )*(strlen(s)+2));
int resize=0;
int i=0;
while(s[i]!='\0'){
// printf("|i %d ",i);
if(s[i]!='('){
re[resize++]=s[i];
}
else{
int index=f(s,i+1,knowledge,knowledgeSize);
if(index==-1){
re[resize++]='?';
}
else{
char *p=knowledge[index][1];
int k=0;
while(p[k]!='\0'){
re[resize++]=p[k++];
}
}
while(s[i]!=')'){
i++;
}
}
i++;
}
re[resize]='\0';
return re;
}
边栏推荐
- Solve the problem of data disorder caused by slow asynchronous interface
- EhLib 数据库记录的下拉选择
- How to implement Devops with automatic tools
- 股票开户流程是什么?使用同花顺手机炒股软件安全吗?
- [advanced C language] array & pointer & array written test questions
- 从RepVgg到MobileOne,含mobileone的代码
- How was MP3 born?
- Spatiotemporal prediction 3-graph transformer
- PMO:比较25种分子优化方法的样本效率
- Bizchart+slider to realize grouping histogram
猜你喜欢
GTEST from ignorance to proficiency (3) what are test suite and test case
TCP三次握手,四次挥手,你真的了解吗?
案例分享|金融业数据运营运维一体化建设
能源势动:电力行业的碳中和该如何实现?
QT—绘制其他问题
Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
Redis has three methods for checking big keys, which are necessary for optimization
Redis03 - network configuration and heartbeat mechanism of redis
玩转gRPC—深入概念与原理
【LeetCode】17、电话号码的字母组合
随机推荐
el-tree结合el-table,树形添加修改操作
Drop down selection of Ehlib database records
并列图的画法,多排多列
# 2156. 查找给定哈希值的子串-后序遍历
[wechat applet] collaborative work and release
ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
[leetcode] 17. Letter combination of telephone number
283. Moving zero-c and language assisted array method
[early knowledge of activities] list of recent activities of livevideostack
Case sharing | integrated construction of data operation and maintenance in the financial industry
Jerry's ad series MIDI function description [chapter]
Spatiotemporal prediction 3-graph transformer
HDU - 2859 Phalanx(DP)
Interviewer: what is XSS attack?
Learning breakout 3 - about energy
WebGIS框架---kalrry
Basic structure of PostgreSQL - table
Open3D 曲面法向量计算
What is the stock account opening process? Is it safe to use flush mobile stock trading software?
解决异步接口慢导致的数据错乱问题