Wednesday, November 18, 2015

443A - Anton and Letters (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
int a[35];

int main()
{
    int i, len, k, count=0;
    string s;
    getline(cin, s);
    len = s.length();
    for(i=0; i<len; i++)
    {
        if(s[i]==',' || s[i]=='}')
        {
            k = s[i-1]-'0';
            if(a[k]==0 && k!=75)
                a[k]++, count++;
        }
    }
    cout << count << endl;
    return 0;
}


447A. DZY Loves Hash (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
int a[305];

int main()
{
    int p, x, n, rem, i;
    int flag=1, cnf = -1;
    cin >> p >> n;
    for(i=0; i<n; i++)
    {
        cin >> x ;
        rem = x%p;
        if(a[rem]!=0 && flag==1)
        {
           cnf = i+1;
           flag =0;
        }
        a[rem]++;
    }
    cout << cnf << endl;
    return 0;
}

Friday, October 9, 2015

Find the Factorial of a Number (Iterative way)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int fact(int n)
{
    int i, ans=1;
    for(i=1; i<=n; i++)
        ans*=i;
    return ans;
}
int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

Find Factorial of a Number (Recursively)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int fact(int n)
{
    if(n==1) return 1;
    return n*fact(n-1);
}

int main()
{
    int n;
    cin>> n;
    cout << fact(n) << endl;
}

1214 - Large Division (LightOJ)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;

int main()
{
    //freopen("in.txt" , "r", stdin);
    string a;
    long long b, rem;
    int test, t, i;
    cin>> test;
    for(t=1; t<=test; t++)
    {
        cin >> a >> b;
        if(b<0)
            b = b*(-1);
        int len = a.length();
        rem=0;
        for(i=0; i<len; i++)
        {
            if(a[i]=='-')
                i=1;
            rem*=10;
            rem+=(a[i]-'0');
            rem%=b;
        }
        if(rem==0)
            printf("Case %d: divisible\n", t);
        else
            printf("Case %d: not divisible\n", t);
    }
}

Saturday, September 12, 2015

1082 - Array Queries (Light OJ)

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MX 10005

const int N = 100000;
int data[N+10], save[4*N+10];
int inf = 1 << 28;
int n;

void build_tree(int node, int st, int en, int a[], int A[])
{
    if(st==en)
    {
        A[node] = a[st];
        return;
    }
    int mid = st+ ((en-st)>>1);
    int left = node<<1;
    int right = (node<<1)+1;
    build_tree(left, st, mid, a, A);
    build_tree(right, mid+1, en, a, A);
    A[node] = min(A[left], A[right]);
}
int query_tree(int node, int st, int en, int a, int b, int A[])
{
    if(a>b)
        return inf;

    if(st==a && en==b)
        return A[node];
    int mid = st+ ((en-st)>>1);
    int left = node<<1;
    int right = (node<<1)+1;

    int l = query_tree(left, st, mid, a, min(b, mid), A);
    int r = query_tree(right ,mid+1, en, max(a, mid+1), b, A);
    return min(l, r);
}

void Build(int tot)
{
    build_tree(1, 1, tot, data, save);
}
int query(int a, int b)
{
    return query_tree(1, 1, n, a, b, save);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    int test, t;
    int q, a, b;
    cin >> test;
    for(t=1; t<=test; t++)
    {
        cin >> n >> q;
        for(int i=1; i<=n; i++)
             scanf("%d", &data[i]);
        Build(n);
        cout << "Case " << t  <<  ":" << endl;
        for(int i=1; i<=q; i++)
        {
           scanf("%d %d", &a, &b);
           printf("%d\n",  query(a, b));
        }
    }
    return 0;
}


Wednesday, September 9, 2015

490B - Queue

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
#define sc(n) scanf("%d", &n)
#define sf scanf
#define pf printf
#define MX 100005
#define pb push_back
#define ff first
#define ss second

typedef long long ll;
int freq[1000005];
int arr[200005];

vector < pair<int, int> > VP;
map<int , int> mp;

int main()
{
    //freopen("in4.txt","r",stdin);
    int i, j, a, b, n;
    int check, check2, inEven=2, inOdd=1;
    cin >> n;
   
    for(i=0; i<n; i++)
    {
        cin >> a >> b;
        VP.pb( make_pair(a, b));
        mp[a] = b;
        freq[VP[i].ff]++;
        freq[VP[i].ss]++;
        if(VP[i].ff==0)
        {
            check = VP[i].ss;
            arr[inEven] = check;
            inEven+=2;
        }
    }
   
    for(int i=0; i<n; i++)
    {
      if(freq[VP[i].ff]==1)
      {
          check2 =  VP[i].ff;
          arr[inOdd] = check2;
          inOdd+=2;
      }
    }
   
    for(int i=0; i<=(n/2); i++)
    {
        arr[inEven] = mp[check] ;
        check = arr[inEven];
            inEven+=2;
        arr[inOdd] = mp[check2];
        check2 = arr[inOdd] ;
            inOdd+=2;
    }
   
    for(int i=1; i<=n; i++)
        cout << arr[i] << " " ;

    return 0;
}


Compare equality of two string in C

#include <stdio.h> #include<string.h> int main() {     char* country = "Bangladesh";     char* country2;     ...