当前位置:网站首页>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;
}
边栏推荐
- NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
- HDU - 1078 fatmouse and cheese (memory search DP)
- HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
- VS2019 C# release下断点调试
- Interviewer: what is XSS attack?
- 如何使用ConcurrentLinkedQueue做一个缓存队列
- Rotary transformer string judgment
- Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
- 机器学习笔记 - 互信息Mutual Information
- PostgreSQL基本结构——表
猜你喜欢

How is the entered query SQL statement executed?

Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history

Why do you have to be familiar with industry and enterprise business when doing Bi development?
![[public class preview]: basis and practice of video quality evaluation](/img/fd/42b98a08b5a0fd89c119f1d1a8fe1b.png)
[public class preview]: basis and practice of video quality evaluation

Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化

KDD2022 | 什么特征进行交互才是有效的?
![Compréhension approfondie du symbole [langue C]](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
Compréhension approfondie du symbole [langue C]

Cloudcompare & open3d DBSCAN clustering (non plug-in)

HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙

Daily question -leetcode1200- minimum absolute difference - array - sort
随机推荐
Representation of confidence interval
Case sharing | integrated construction of data operation and maintenance in the financial industry
Shutter WebView example
HDU - 2859 Phalanx(DP)
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Flink1.13 SQL basic syntax (I) DDL, DML
Jerry's ad series MIDI function description [chapter]
PostgreSQL基本结构——表
案例分享|金融业数据运营运维一体化建设
输入的查询SQL语句,是如何执行的?
[weekly translation go] how to code in go series articles are online!!
QT - plot other problems
电话加密,中间4为****代替
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
Bizchart+slider to realize grouping histogram
置信区间的画法
Kubedm initialization error: [error cri]: container runtime is not running
MongoDB聚合操作总结
Bookmark
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe to open an account online