热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

牛客括号字符串的有效性和最长有效长度

题目链接:https:www.nowcoder.compractice335acafb6d5141b7873c4b0f24d53c57?tpId101&tqId331

题目链接:https://www.nowcoder.com/practice/335acafb6d5141b7873c4b0f24d53c57?tpId=101&tqId=33195&tPage=1&rp=1&ru=/ta/programmer-code-interview-guide&qru=/ta/programmer-code-interview-guide/question-ranking

题目大意:

  略。

分析:

略。

代码如下:

1 #include
2 using namespace std;
3
4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
5 #define Rep(i,n) for (int i &#61; 0; i <(int)(n); &#43;&#43;i)
6 #define For(i,s,t) for (int i &#61; (int)(s); i <&#61; (int)(t); &#43;&#43;i)
7 #define rFor(i,t,s) for (int i &#61; (int)(t); i >&#61; (int)(s); --i)
8 #define ForLL(i, s, t) for (LL i &#61; LL(s); i <&#61; LL(t); &#43;&#43;i)
9 #define rForLL(i, t, s) for (LL i &#61; LL(t); i >&#61; LL(s); --i)
10 #define foreach(i,c) for (__typeof(c.begin()) i &#61; c.begin(); i !&#61; c.end(); &#43;&#43;i)
11 #define rforeach(i,c) for (__typeof(c.rbegin()) i &#61; c.rbegin(); i !&#61; c.rend(); &#43;&#43;i)
12
13 #define pr(x) cout <<#x <<" &#61; " < 14 #define prln(x) cout <<#x <<" &#61; " < 15
16 #define LOWBIT(x) ((x)&(-x))
17
18 #define ALL(x) x.begin(),x.end()
19 #define INS(x) inserter(x,x.begin())
20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
24
25 #define ms0(a) memset(a,0,sizeof(a))
26 #define msI(a) memset(a,0x3f,sizeof(a))
27 #define msM(a) memset(a,-1,sizeof(a))
28
29 #define MP make_pair
30 #define PB push_back
31 #define ft first
32 #define sd second
33
34 template
35 istream &operator>>(istream &in, pair &p) {
36 in >> p.first >> p.second;
37 return in;
38 }
39
40 template
41 istream &operator>>(istream &in, vector &v) {
42 for (auto &x: v)
43 in >> x;
44 return in;
45 }
46
47 template
48 ostream &operator<<(ostream &out, vector &v) {
49 Rep(i, v.size()) out <" \n"[i &#61;&#61; v.size() - 1];
50 return out;
51 }
52
53 template
54 ostream &operator<<(ostream &out, const std::pair &p) {
55 out <<"[" <", " <"]" <<"\n";
56 return out;
57 }
58
59 inline int gc(){
60 static const int BUF &#61; 1e7;
61 static char buf[BUF], *bg &#61; buf &#43; BUF, *ed &#61; bg;
62
63 if(bg &#61;&#61; ed) fread(bg &#61; buf, 1, BUF, stdin);
64 return *bg&#43;&#43;;
65 }
66
67 inline int ri(){
68 int x &#61; 0, f &#61; 1, c &#61; gc();
69 for(; c<48||c>57; f &#61; c&#61;&#61;&#39;-&#39;?-1:f, c&#61;gc());
70 for(; c>47&&c<58; x &#61; x*10 &#43; c - 48, c&#61;gc());
71 return x*f;
72 }
73
74 template<class T>
75 inline string toString(T x) {
76 ostringstream sout;
77 sout << x;
78 return sout.str();
79 }
80
81 inline int toInt(string s) {
82 int v;
83 istringstream sin(s);
84 sin >> v;
85 return v;
86 }
87
88 //min <&#61; aim <&#61; max
89 template
90 inline bool BETWEEN(const T aim, const T min, const T max) {
91 return min <&#61; aim && aim <&#61; max;
92 }
93
94 typedef unsigned int uI;
95 typedef long long LL;
96 typedef unsigned long long uLL;
97 typedef vector<int > VI;
98 typedef vector<bool > VB;
99 typedef vector<char > VC;
100 typedef vector<double > VD;
101 typedef vector<string > VS;
102 typedef vector VL;
103 typedef vector VVI;
104 typedef vector VVB;
105 typedef vector VVS;
106 typedef vector VVL;
107 typedef vector VVVI;
108 typedef vector VVVL;
109 typedef pair<int, int > PII;
110 typedef pair PLL;
111 typedef pair<int, string > PIS;
112 typedef pair<string, int > PSI;
113 typedef pair<string, string > PSS;
114 typedef pair<double, double > PDD;
115 typedef vector VPII;
116 typedef vector VPLL;
117 typedef vector VVPII;
118 typedef vector VVPLL;
119 typedef vector VVS;
120 typedef map<int, int > MII;
121 typedef unordered_map<int, int > uMII;
122 typedef map MLL;
123 typedef map<string, int > MSI;
124 typedef map<int, string > MIS;
125 typedef multiset<int > mSI;
126 typedef multiset<char > mSC;
127 typedef set<int > SI;
128 typedef stack<int > SKI;
129 typedef deque<int > DQI;
130 typedef queue<int > QI;
131 typedef priority_queue<int > PQIMax;
132 typedef priority_queue<int, VI, greater<int > > PQIMin;
133 const double EPS &#61; 1e-8;
134 const LL inf &#61; 0x7fffffff;
135 const LL infLL &#61; 0x7fffffffffffffffLL;
136 const LL mod &#61; 1e9 &#43; 7;
137 const int maxN &#61; 1e5 &#43; 7;
138 const LL ONE &#61; 1;
139 const LL evenBits &#61; 0xaaaaaaaaaaaaaaaa;
140 const LL oddBits &#61; 0x5555555555555555;
141
142 int N, M, ans;
143 string str;
144 // dp[i]表示str[0~i-1]以str[i - 1]结尾的最长有效长度
145 int dp[maxN];
146
147 int main() {
148 //freopen("MyOutput.txt","w",stdout);
149 //freopen("input.txt","r",stdin);
150 //INIT();
151 cin >> str;
152 N &#61; str.size();
153 dp[0] &#61; 0;
154
155 For(i, 1, N) {
156 if(str[i - 1] &#61;&#61; &#39;)&#39;) {
157 int tmp &#61; i - 2 - dp[i - 1];
158 if(tmp >&#61; 0 && str[tmp] &#61;&#61; &#39;(&#39;) {
159 dp[i] &#61; dp[i - 1] &#43; 2 &#43; dp[tmp];
160 }
161 }
162 ans &#61; max(ans, dp[i]);
163 }
164
165 cout < endl;
166 return 0;
167 }

View Code

 

转:https://www.cnblogs.com/zaq19970105/p/11418261.html



推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • ***byte(字节)根据长度转成kb(千字节)和mb(兆字节)**parambytes*return*publicstaticStringbytes2kb(longbytes){ ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 合并列值-合并为一列问题需求:createtabletab(Aint,Bint,Cint)inserttabselect1,2,3unionallsel ... [详细]
author-avatar
wyf叶子_594
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有