void DefaultSCgObjectBuilder::buildObjects(const AbstractSCgObjectBuilder::TypeToObjectsMap& objects) { SCgObjectInfo* info; // parse nodes foreach(info, objects[SCgNode::Type]) buildNode(static_cast<SCgNodeInfo*>(info)); // parse pairs foreach(info, objects[SCgPair::Type]) buildPair(static_cast<SCgPairInfo*>(info)); // parse buses foreach(info, objects[SCgBus::Type]) buildBus(static_cast<SCgBusInfo*>(info)); // parse contours foreach(info, objects[SCgContour::Type]) buildContour(static_cast<SCgContourInfo*>(info)); // set parents relation ParentChildMap::iterator parentIt; for (parentIt = mParentChild.begin(); parentIt != mParentChild.end(); parentIt++) { if (!mId2SCgObj.contains(parentIt.value())) continue; SCgObject *parent = mId2SCgObj[parentIt.value()]; SCgObject *child = mId2SCgObj[parentIt.key()]; child->setParentItem(parent); } // holds true, if there are errors while setting begin and end objects. bool isConnectedDuty = false; // set begin and end objects for pairs foreach(SCgObjectInfo* info, objects[SCgPair::Type]) { SCgPairInfo* pairInfo = static_cast<SCgPairInfo*>(info); SCgPair *pair = static_cast<SCgPair*>(mId2SCgObj[pairInfo->id()]); // we can't build pair without begin or end objects if (!mId2SCgObj.contains(pairInfo->beginObjectId()) || !mId2SCgObj.contains(pairInfo->endObjectId())) { mErrors.append(QObject::tr("Can't find begin or end object for pair id=\"%1\"") .arg(pairInfo->id())); mId2SCgObj.remove(pairInfo->id()); isConnectedDuty = true; delete pair; continue; } SCgObject *begObject = mId2SCgObj[pairInfo->beginObjectId()]; SCgObject *endObject = mId2SCgObj[pairInfo->endObjectId()]; pair->setBeginObject(begObject); pair->setEndObject(endObject); }
int longestValidParentheses(string s) { buildPair(s); int current = 0, ans = 0, idx = 0; while (idx < s.size()) { if (pair.count(idx)) { current += pair[idx] - idx + 1; idx = pair[idx] + 1; } else { ans = max(ans, current); current = 0; idx++; } } return max(ans, current); }