当前位置:网站首页>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;
}
边栏推荐
- Master the use of auto analyze in data warehouse
- 开户哪家券商比较好?网上开户安全吗
- el-tree结合el-table,树形添加修改操作
- 类方法和类变量的使用
- 服装企业为什么要谈信息化?
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- Bookmark
- Flutter 返回按钮的监听
- How was MP3 born?
- How is the entered query SQL statement executed?
猜你喜欢

Master the use of auto analyze in data warehouse

【LeetCode】17、电话号码的字母组合

力扣98:验证二叉搜索树

QT - plot other problems

历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享

Application practice | Shuhai supply chain construction of data center based on Apache Doris

Redis03 - network configuration and heartbeat mechanism of redis

能源势动:电力行业的碳中和该如何实现?
![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[weekly translation go] how to code in go series articles are online!!

Bizchart+slider to realize grouping histogram
随机推荐
GTEST from ignorance to proficiency (3) what are test suite and test case
The drawing method of side-by-side diagram, multi row and multi column
HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
Case sharing | integrated construction of data operation and maintenance in the financial industry
Redis 排查大 key 的3种方法,优化必备
Bookmark
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
做BI开发,为什么一定要熟悉行业和企业业务?
Go language loop statement (3 in Lesson 10)
vim 从嫌弃到依赖(23)——最后的闲扯
Open3D 曲面法向量计算
VIM from dislike to dependence (23) -- the last gossip
Acwing 2022 daily question
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
解决异步接口慢导致的数据错乱问题
[leetcode] 17. Letter combination of telephone number
哈希表(Hash Tabel)
大厂的广告系统升级,怎能少了大模型的身影
TCP shakes hands three times and waves four times. Do you really understand?
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远