bool is_semidivisible(unsigned long long n) { //cout << n << " = " << lps(n) << " :: " << ups(n) << endl; if(n%lps(n)==0 && n%ups(n)==0) return false; else if(n%lps(n)==0) return true; else if(n%ups(n)==0) return true; else return false; }
void Streebog::compress_64(const uint64_t M[], bool last_block) { uint64_t N = force_le(last_block ? 0ULL : m_count); uint64_t hN[8]; uint64_t A[8]; copy_mem(hN, m_h.data(), 8); hN[0] ^= N; lps(hN); copy_mem(A, hN, 8); for(size_t i = 0; i != 8; ++i) { hN[i] ^= M[i]; } for(size_t i = 0; i < 12; ++i) { for(size_t j = 0; j != 8; ++j) A[j] ^= force_le(STREEBOG_C[i][j]); lps(A); lps(hN); for(size_t j = 0; j != 8; ++j) hN[j] ^= A[j]; } for(size_t i = 0; i != 8; ++i) { m_h[i] ^= hN[i] ^ M[i]; } if(!last_block) { uint64_t carry = 0; for(int i = 0; i < 8; i++) { const uint64_t m = force_le(M[i]); const uint64_t hi = force_le(m_S[i]); const uint64_t t = hi + m; m_S[i] = force_le(t + carry); carry = (t < hi ? 1 : 0) | (t < m ? 1 : 0); } } }
vector<int> longest_prefix_suffix(string p) { int j=0, i=1; vector<int> lps(p.size()); lps[0] = 0; while(i<p.size()) { if(p[i] == p[j]) { lps[i] = lps[i-1] + 1; j++; } else { while(j>0 && p[i] != p[j]) { j = lps[j-1]; } if(p[i] == p[j]) { lps[i] = lps[j] + 1; j++; } else lps[i] = 0; } i++; } /*for(int k=0; k<p.size(); k++) cout << lps[k] << " ";*/ return lps; }
void KMPsearch(const string &pat, const string &txt) { int m = pat.size(), n = txt.size(); vector<int> lps(m); computeLPS(pat, lps); int i = 0, j = 0; while(i < n) { if(pat[j] == txt[i]) { ++j; ++i; } if(j == m) { cout << "found pattern at " << i - j << endl; j = lps[j - 1]; } else if(i < n && pat[j] != txt[i]) { if(j != 0) j = lps[j - 1]; //until pa[lps[j - 1] - 1] already match. next index pa[lps[j - 1]] else i = i + 1; } } }
/* Driver program to test above functions */ int main() { char seq[] = "abacdfgdcaba"; int n = strlen(seq); printf ("The lnegth of the LPS is %d", lps(seq)); getchar(); return 0; }
/* Driver program to test above functions */ int main() { char seq[] = "GEEKS FOR GEEKS"; int n = strlen(seq); printf ("The lnegth of the LPS is %d", lps(seq)); getchar(); return 0; }
int main() { char seq[1024]; scanf("%s",seq); int n = strlen(seq); printf ("%d", lps(seq)); getchar(); return 0; }
int sample_topic() { std::vector<double> lps(num_topics_); for(int topic_idx = 0; topic_idx < num_topics_; topic_idx++) { double inner = word_dmc.log_u_conditional(current_word_index, c_topic[topic_idx], c_topic_sums[topic_idx], 1); inner += topic_dmc.log_u_conditional(topic_idx, *c_doc_topic_ptr, num_docs, 1); lps[topic_idx] = inner; } return dmc::cat::log_u_sample(lps); }
vector<int> kmpProcess(string& needle) { int n = needle.length(); vector<int> lps(n, 0); for (int i = 1, len = 0; i < n; ) { if (needle[i] == needle[len]) lps[i++] = ++len; else if (len) len = lps[len - 1]; else lps[i++] = 0; } return lps; }
int main(int argc,char* argv[]) { LidarProcessOctree lpo(argv[1],512*1024*1024); #if 0 Misc::Timer t; { // BinaryPointSaver bps(argv[2]); double scale[3]={0.001,0.001,0.001}; double offset[3]; for(int i=0;i<3;++i) offset[i]=lpo.getDomain().getCenter(i); LasPointSaver lps(argv[2],scale,offset); // PointCounter pc; Box box(Box::Point(2.04446e6,617053.0,-1000.0),Box::Point(2.04446e6+100.0,617053.0+100.0,1000.0)); lpo.processPointsInBox(box,lps); // std::cout<<lps.getNumPoints()<<std::endl; } t.elapse(); std::cout<<"Done in "<<t.getTime()*1000.0<<" ms"<<std::endl; #elif 0 PointSaver ps(argv[2]); Box box=Box::full; for(int i=0;i<2;++i) { box.min[i]=atof(argv[3+i]); box.max[i]=atof(argv[5+i]); } lpo.processPointsInBox(box,ps); #elif 0 PointCounter pc; lpo.processPoints(pc); std::cout<<pc.getNumPoints()<<std::endl; #else Scalar radius=Scalar(0.1); PointDensityCalculator pdc(lpo,radius,4); #if 0 Box box; box.min=Point(930,530,0); box.max=Point(970,570,100); lpo.processPointsInBox(box,pdc); #else // lpo.processPoints(pdc); lpo.processNodesPostfix(pdc); #endif std::cout<<"Number of processed points: "<<pdc.numPoints<<std::endl; std::cout<<"Total number of found neighbors: "<<pdc.totalNumNeighbors<<std::endl; std::cout<<"Total number of loaded octree nodes: "<<lpo.getNumSubdivideCalls()<<", "<<lpo.getNumLoadedNodes()<<std::endl; std::cout<<"Average point density in 1/m^3: "<<pdc.totalNumNeighbors/(pdc.numPoints*4.0/3.0*3.141592654*radius*radius*radius)<<std::endl; #endif return 0; }
int main() { char string[]="aibohphobia"; int l=sizeof(string)-1; int LPS[MAX][MAX]; char p[MAX]; memset(LPS,0,sizeof(LPS)); printf("Length=%d\n",lps(string,l,LPS)); printf("String=%s",printLPS(p,string,l,LPS)); getchar(); }
void FSISPOptimizerOLD::calculateKnapsackFunctionAssignment(void) { if(ilp_knapsack_formulation.empty()) { generateKnapsackILPFormulation(); } writeKnapsackILPFile(); LpSolver lps(ilp_knapsack_formulation, conf->getString(CONF_LP_SOLVE_PARAMETERS)); // LpSolver lps(ilpfile_name, conf->getString(CONF_LP_SOLVE_PARAMETERS)); setAssignment(lps.lpSolve()); }
string shortestPalindrome(string s) { string rev(s); reverse(rev.begin(), rev.end()); string str = s + "#" + rev; int n = str.size(); int len = 0; int i = 1; vector<int> lps(n); while (i < n) { if (str[i] == str[len]) { lps[i++] = ++len; } else if (len != 0) { len = lps[len - 1]; } else { lps[i++] = 0; } } return rev.substr(0, rev.size() - len) + s; }
// the straightforward solution is in O(N^2); using KMP's preprocessing algorithm, // we can reduce this to O(N) int longest_palindrome_prefix(const string &s) const { /** * The LPS computation can determine, at any given index i in a string S, the maximum suffix length that * make up a suffix equal to the prefix. For example: S = "acexxxaceyyy": at S[6], S[7], and S[8] will be * marked with "1", "2", and "3" respectively because "a", "ac", and "ace" at this points in the string * make up substrings whose suffixes equal to the string's prefix. This computation can be done in one * linear scan of the string in O(N) time, using a secondary integer array in O(N) space. * * For our purpose in finding the longest palindrome prefix of a string, the idea is simple: * if we reverse the string, then appending it to the original string (after a special marker), * the palindromic prefix will show up at the end of the compound string! If we then apply the above algorithm, * by the end of the linear scan, we'll have a number that correspond to the maximum suffix length of * the entire compound string, which correspond to a suffix = prefix. And since a palindromic prefix, when * reversed and appended, will show up as the suffix, we will conveniently have computed the maximum * length of the palindromic prefix! * * For example: consider the string S = "abbaaax". The longest palindrome prefix is "abba". * 1. Create S' = "abbaaax#xaaabba" * 2. Compute LPS: lps[] = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 2, 3, 4 } * from S'[] = {'a','b','b','a','a','a','x','#','x','a','a','a','b','b','a'} * 3. The last element of LPS, 4, is our longest palindrome prefix length! */ string kmprev = s; std::reverse(kmprev.begin(), kmprev.end()); string kmp = s + "#" + kmprev; vector<int> lps(kmp.size(), 0); // lps[i] = longest suffix length for substring kmp[0..i] where the suffix == prefix for (int i = 1; i < (int)lps.size(); ++i) { int prev_idx = lps[i - 1]; while (prev_idx > 0 && kmp[i] != kmp[prev_idx]) { prev_idx = lps[prev_idx - 1]; } lps[i] = prev_idx + (kmp[i] == kmp[prev_idx] ? 1 : 0); } // after KMP's LPS preprocessing, the last index of the LPS array will contain the longest palindrome prefix' length! return lps[lps.size() - 1]; }
int strStr(string haystack, string needle) { vector<int> lps(needle.size(), 0); for(int i = 1; i < needle.size(); ++i) { int j = lps[i - 1]; while(j > 0 && needle[i] != needle[j]) j = lps[j - 1]; lps[i] = (j += needle[i] == needle[j]); } int i = 0, j = 0; while(i < haystack.size() && j < needle.size()) { if(j > 0 && haystack[i] != needle[j]) j = lps[j - 1]; else { j += haystack[i] == needle[j]; ++i; } } return (j == needle.size()) ? i - j : -1; }