本文整理汇总了C++中unordered_set::count方法的典型用法代码示例。如果您正苦于以下问题:C++ unordered_set::count方法的具体用法?C++ unordered_set::count怎么用?C++ unordered_set::count使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unordered_set
的用法示例。
在下文中一共展示了unordered_set::count方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: buildTree
bool buildTree(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict,
unordered_map<string, vector<string>> &tree, bool reversed){
if (forward.empty())
return false;
if (forward.size() > backward.size())
return buildTree(backward, forward, dict, tree, !reversed);
for (auto &word : forward)
dict.erase(word);
for (auto &word : backward)
dict.erase(word);
unordered_set<string> nextLevel;
bool done = false; // guarantee shortest ladder
for (auto &it : forward){ //traverse each word in the forward -> the current level of the tree;
string word = it;
for (auto &c : word){
char c0 = c; //store the original;
for (c = 'a'; c <= 'z'; ++c) //try each case;
if (c != c0){ //avoid futile checking;
//using count is an accelerating method;
if (backward.count(word)){ // count is O(1) while isChangedByOne is O(size(word)* size(backward))
done = true;
//keep the tree generation direction consistent;
!reversed ? tree[it].push_back(word) : tree[word].push_back(it);
}
else if (!done && dict.count(word)){
nextLevel.insert(word);
!reversed ? tree[it].push_back(word) : tree[word].push_back(it);
}
}
c = c0; //restore the word;
}
}
return done || buildTree(nextLevel, backward, dict, tree, reversed);
}
示例2: wordBreak
bool wordBreak(string s, unordered_set<string> &dict) {
if (s.size() == 0){
return true;
}
if (dict.size() == 0){
return false;
}
bool* f = (bool*)malloc(sizeof(bool)*s.size());
for(int i = 0; i < s.size(); i++){
f[i] = false;
}
for(int i = 0; i < s.size(); i++){
f[i] = dict.count(s.substr(0,i+1));
if (f[i]){
continue;
}
for (int j = 0; j < i; ++j){
if (f[j] && dict.count(s.substr(j+1,i-j))){
f[i] = true;
break;
}
}
}
return f[s.size() - 1];
}
示例3: helper
bool helper(unordered_set<string>& word1, unordered_set<string>& word2, unordered_set<string>& dict){
if(word1.empty()) return 0;
if(word1.size() > word2.size()) return helper(word2, word1, dict);
for(auto v : word1) dict.erase(v);
for(auto v : word2) dict.erase(v);
++res;
bool reach = false;
unordered_set<string> word3;
for(auto it = word1.begin(); it != word1.end(); ++it){
string word = *it;
for(int i = 0; i < word.size(); ++i){
char letter = word[i];
for(int k = 0; k < 26; ++k){
word[i] = 'a'+k;
if(word2.count(word)){
reach = true;
return reach;
}
if(dict.count(word) && !reach){
word3.insert(word);
}
}
word[i] = letter;
}
}
return reach || helper(word2, word3, dict);
}
示例4: wordBreak
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
int n = s.length();
// 要先判断是否能分割,能的话再进行记录,否则TLE
if(!wordBreak1(s,wordDict))
return vector<string>();
vector<int> f(n,0);
vector<vector<string>> ans(n,vector<string>());
if(wordDict.count(s.substr(0,1))){
f[0] = 1;
ans[0].push_back(s.substr(0,1));
}
string tmp;
for(int i = 1;i < n;++i){
for(int j = 0;j < i;++j){
tmp = s.substr(j+1,i-j);
if(f[j] && wordDict.count(tmp)){
f[i] = 1;
for(int k = 0;k < ans[j].size();++k)
ans[i].push_back(ans[j][k]+" "+tmp);
}
}
tmp = s.substr(0,i+1);
if(wordDict.count(tmp)){
ans[i].push_back(tmp);
f[i] = 1;
}
}
return ans[n-1];
}
示例5: doit
vector<string> doit(string s,unordered_set<string> &dict) {
if(ht.count(s))
return ht[s];
vector<string> ret;
// if s is contained in dict, it is added into results; but it may be decomposed further
if(dict.count(s))
ret.push_back(s);
int n = s.size();
for(int i=1;i<n;++i) {
string prefix = s.substr(0,i);
if(dict.count(prefix)==1) {
vector<string> ret2 = doit(s.substr(i),dict);
if(!ret2.empty()) {
for(string str : ret2) {
string tmp = prefix + " " + str;
ret.push_back(tmp);
}
// break;
}
}
}
ht[s] = ret;
return ret;
}
示例6: ladderLength
//If there is no '&' before set, the time cost will be tripled;
int ladderLength(unordered_set<string>& bList, unordered_set<string>& eList, unordered_set<string>& wordList, int len)
{
if(bList.empty())
return 0;
if(bList.size() > eList.size())
return ladderLength(eList, bList, wordList, len);
unordered_set<string> tmpList;
for(auto it=bList.begin(); it!=bList.end(); it++)
{
string cur = *it;
for(int i=0; i<cur.size(); i++)
{
char c0 = cur[i];
for(char c='a'; c<='z'; c++)
{
cur[i]=c;
if(eList.count(cur)) return len+1;
if(wordList.count(cur))
{
tmpList.insert(cur);
wordList.erase(cur);
}
}
cur[i] = c0;
}
}
return ladderLength(tmpList, eList, wordList, len+1);
}
示例7: canCross
bool canCross(unordered_set<int>& units, map<pos, bool>& flags, int end, int start, int k) {
if (start == end) {
return true;
}
if (k - 1 > 0 && start + k - 1 <= end && units.count(start + k - 1)) {
auto it = flags.find({ start + k - 1, k - 1 });
if (it == flags.end()) {
bool can = canCross(units, flags, end, start + k - 1, k - 1);
if (can) {
return true;
}
}
}
if (start + k <= end && units.count(start + k)) {
auto it = flags.find({ start + k, k });
if (it == flags.end()) {
bool can = canCross(units, flags, end, start + k, k);
if (can) {
return true;
}
}
}
if (start + k + 1 <= end && units.count(start + k + 1)) {
auto it = flags.find({ start + k + 1, k + 1 });
if (it == flags.end()) {
bool can = canCross(units, flags, end, start + k + 1, k + 1);
if (can) {
return true;
}
}
}
flags[{start, k}] = false;
return false;
}
示例8: findWords
vector<string> findWords(vector<string>& words) {
vector<string> res;
int last = -1;
for (auto &word : words) {
bool valid = true;
for (auto c : word) {
c = tolower(c);
if (row1.count(c)) {
if (last != -1 && last != 1) {
valid = false;
break;
}
last = 1;
} else if (row2.count(c)) {
if (last != -1 && last != 2) {
valid = false;
break;
}
last = 2;
} else {
if (last != -1 && last != 3) {
valid = false;
break;
}
last = 3;
}
}
if (valid) res.push_back(word);
}
return res;
}
示例9: wordBreak
bool wordBreak(string s){
if (cache.find(s) != cache.end()) return cache[s];
if (Dict->count(s)) return cache[s] = true;
for (int i = 1; i < s.length(); i++){
string left = s.substr(0, i);
if (Dict->count(left) && wordBreak(s.substr(i, s.length() - i))) return cache[s] = true;
}
return cache[s] = false;
}
示例10: buildLoopMemberSet
void SESELoop::buildLoopMemberSet(BasicBlock& backEdgeDestination, const unordered_multimap<BasicBlock*, BasicBlock*>& destToOrigin, unordered_set<BasicBlock*>& members, unordered_set<BasicBlock*>& entries, unordered_set<BasicBlock*>& exits)
{
// Build paths to back-edge start nodes.
unordered_set<BasicBlock*> sinkNodeSet;
auto range = destToOrigin.equal_range(&backEdgeDestination);
for (auto iter = range.first; iter != range.second; iter++)
{
sinkNodeSet.insert(iter->second);
}
auto pathsToBackNodes = findPathsToSinkNodes(&backEdgeDestination, sinkNodeSet);
// Build initial loop membership set
for (const auto& path : pathsToBackNodes)
{
members.insert(path.begin(), path.end());
}
// The path-to-sink-nodes algorithm won't follow back edges. Because of that, if the cycle contains a
// sub-cycle, we need to add its member nodes. This is probably handled by the loop membership refinement
// step from the "No More Gotos" paper, but as noted below, we don't use that step.
unordered_set<BasicBlock*> newMembers;
for (BasicBlock* bb : members)
{
auto range = loopMembers.equal_range(bb);
for (auto iter = range.first; iter != range.second; iter++)
{
newMembers.insert(iter->second);
}
}
members.insert(newMembers.begin(), newMembers.end());
for (BasicBlock* member : members)
{
loopMembers.insert({&backEdgeDestination, member});
for (BasicBlock* pred : predecessors(member))
{
if (members.count(pred) == 0)
{
entries.insert(member);
}
}
for (BasicBlock* succ : successors(member))
{
if (members.count(succ) == 0)
{
exits.insert(succ);
}
}
}
}
示例11: wordBreak
bool wordBreak(string s, unordered_set<string> &dict) {
int stringLength = s.size();
if (stringLength == 0 || dict.count(s) > 0) return true;
int i;
for (i = 1; i < stringLength; i++) {
string word = s.substr(0, i);
string remain = s.substr(i, stringLength-i);
std::cout << "word: " << word << " remain: " << remain << std::endl;
if (dict.count(word) > 0) if (wordBreak(remain, dict)) return true; // cannot return wordBreak(remain, dict) directly.
if (dict.count(remain) > 0) if (return wordBreak(word, dict)) return true;
}
return false;
}
示例12: dfs
bool dfs(char node, unordered_set<char> &visited, stack<char> &stk, unordered_map<char, unordered_set<char>> &links, unordered_set<char> path)
{
visited.insert(node);
path.insert(node);
for(auto nb:links[node])
{
if(path.count(nb)==1) return false;
if(visited.count(nb)==0)
if(!dfs(nb, visited, stk, links, path)) return false;
}
stk.push(node);
return true;
}
示例13: f
bool wordBreak1(string s, unordered_set<string>& wordDict) {
int n = s.length();
// f[i]表示以s[i]为结尾的子串是否能够拆分
vector<int> f(n+1,0);
f[0] = wordDict.count(s.substr(0,1));
for(int i = 1;i < n;++i){
for(int j = 0;j < i;++j){
if(f[j] && wordDict.count(s.substr(j+1,i-j)))
f[i] = 1;
}
if(wordDict.count(s.substr(0,i+1))) f[i] = 1;
}
return f[n-1];
}
示例14: getNext
void getNext(string s, unordered_set<string> &dict, unordered_set<string> &isVisited, unordered_set<string> &nextStrs) {
for(int i = 0; i < s.length(); ++i) {
string nextStr(s);
for(char c = 'a'; c <= 'z'; ++c) {
if(c != nextStr[i]) {
swap(c, nextStr[i]);
if(dict.count(nextStr) && !isVisited.count(nextStr)) {
nextStrs.insert(nextStr);
isVisited.insert(nextStr);
}
swap(c, nextStr[i]);
}
}
}
}
示例15: dfs
void dfs(vector<vector<char> >& board, int i, int j, string candidate, vector<vector<bool> >& visited, vector<string>& ans) {
if(prefix.count(candidate) == 0)
return;
if(dict.count(candidate))
ans.push_back(candidate);
for(int dir = 0; dir < 4; dir++) {
int ii = i + dx[dir];
int jj = j + dy[dir];
if(ii < 0 || jj < 0 || ii >= m || jj >= n || visited[ii][jj])
continue;
visited[ii][jj] = true;
dfs(board, ii, jj, candidate + board[ii][jj], visited, ans);
visited[ii][jj] = false;
}
}