Posts

Showing posts from 2017

Trie--HackerRank - contacts

#include<bits/stdc++.h> #include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<istream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node {     int c=0;     bool mark;     node* next[26+1];     node()     {         mark=false;         for(int i=0; i<26; i++)         {             next[i]=NULL;         }     } }; node* root=NULL; void Insert(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'a';         if(newnode->next[id]==NULL)         {             newnode->next[id]=new node();         }         newnode=newnode->next[id];         newnode->c++;     }     newnode->mark=1; } int Search(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'a';         if(newnode->next[id]==NULL)         {             return 0;         }         n

Trie---HackerRank no-prefix-set

#include<bits/stdc++.h> using namespace std; int k,id; string ss; struct node {     bool m;     node* next[26+1];     node()     {         m=false;         for(int i=0; i<26; i++)         {             next[i]=NULL;         }     } }; node *root =NULL; void Insert(string s,int len) {     node* a=root;     int x=len-1;     for(int i=0; i<len; i++)     {         int id = s[i]-'a';         if(a->next[id]!=NULL && i==x)         {             k=1;         }         if(a->next[id]==NULL)         {             a->next[id]=new node();         }         a=a->next[id];         if(a->m==true)         {             k=1;         }     }     a->m=1; } bool Search(string s,int len) {     node *a=root;     for(int i=0; i<len; i++)     {         int id = s[i]-'a';         if(a->next[id]==NULL)         {             return false;         }         a=a->next[id];     }     return a->m

Trie--UVA 11362-----Phone List

#include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<istream> #include<stdio.h> #include<stdlib.h> using namespace std; int id,m; struct node {     bool mark;     node* next[11];     node()     {         mark=false;         for(int i=0; i<10; i++)         {             next[i]=NULL;         }     } }; node* root=NULL; void Insert(string s) {     int nay=0;     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'0';         if(newnode->next[id]==NULL)         {             nay=1;             newnode->next[id]=new node();         }         if(newnode->mark==1)         {             m=0;         }         newnode=newnode->next[id];     }     if(nay==0)     {         m=0;     }     newnode->mark=1; } bool Search(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'0';  

Converting string and int for using stringstream

#include<bits/stdc++.h> using namespace std; #define ll long long string int_to_str(int n) {     stringstream ss;     ss<<n;     return ss.str(); } int str_to_int(string s) {     stringstream ss(s);     int n;     ss>>n;     return n; } int main() {     ios_base::sync_with_stdio(0);     string s;     cout<<"Input string : ";     cin>>s;     int n;     cout<<"Input integer : ";     cin>>n;     int m=str_to_int(s);     string p=int_to_str(n);     cout<<"String to integer convert: "<<m<<endl;     cout<<"Integer to string convert: "<<p<<endl; }

Uva-10924 - Prime Words

#include<bits/stdc++.h> using namespace std; vector<int>v; bool ar[10005]; bool isprime(int x) {     if(x<10005)     {         return ar[x];     }     for(int i=2; i<sqrt(10005); i++)     {         if(x%ar[i]==0)         {             return false;         }     }     return true; } void seive(int n) {     for(int i=4; i<=n; i+=2)     {         ar[i]=1;     }     for(int i=3; i*i<=n; i+=2)     {         if(!ar[i])         {             for(int j=i*i; j<=n; j=j+i)             {                 ar[j]=1;             }         }     }     //cout<<n<<endl;     if(isprime(n))     {         cout<<"It is not a prime word."<<endl;     }     else     {         cout<<"It is a prime word."<<endl;     } } int main() {     char c[105];     while(cin>>c)     {         int l=strlen(c),s=0;         for(int i=0; i<l; i++)         {             if(c[i]>=&#

Prime Number generator Using Sieve Algorithms

Prime Number Calculate Using Sieve Algorithms ********Problem 1********** You are given N, find the Nth prime number... N:B: 1 is not a prime number Input Format Only a number N Constraints 1 <= N <= 107 Output Format Y where Y is the prime number which is at Nth position. Print "invalid position" when Nth position prime is greater than 107. Sample Input 0 1 Sample Output 0 2 Explanation 0 first prime number is 2 Sample Input 1 5 Sample Output 1 11 Explanation 1 5th prime number is 11 ********Solve******** #include<bits/stdc++.h> using namespace std; #define ll long long vector<int>p; bitset<10000005>bs; bool ar[1000005]; bool isprime(long long x) {     if(x<=10000005)     {         return bs[x];     }     for(long long i=0; i<p.size(); i++)     {         if(x%p[i]==0)         {             return false;         }     }     return true; } void seive(long long n) {     long long i,j;     b