Saturday 29 August 2015

UVALive 3470 Pascal Library (POJ 2864, Regionals 2005 >> Latin America - South America )

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, d, sum[102], x;
    string s;
    char ch;
    while(cin>>n>>d && n)
    {
        CLR(sum);
        for(int i=1;i<=d;i++)
            for(int j=1;j<=n;j++)
            {
                cin>>x;
                sum[j]+=x;
            }
        bool chk=false;
        for(int i=1;i<=n;i++)
            if(sum[i]==d) chk=true;
        if(chk) cout<<"yes\n";
        else    cout<<"no\n";
    }
return 0;
}

Sunday 16 August 2015

UVA 12582 Wedding of Sultan (UVALive 6201, Regionals 2012 >> Asia - Dhaka)

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    int sl, tc,kk=1, n, arr[30];
    string s;
    cin>>tc; 
    while(tc--)
    {
        CLR(arr);
        cin>>s;
        sl=s.length();
        stack<char>st;
        st.push(s[0]);
        st.push(s[1]);
        for(int i=2;i<sl-1;i++)
        {
            char tmp=st.top();
            if(st.top()==s[i])
            {
                arr[s[i]-'A']++;
                st.pop();
                arr[st.top()-'A']++;
            }
            else st.push(s[i]);
        }
        cout<<"Case "<<kk++<<endl;
        for(int i=0;i<26;i++)
            if(arr[i])
                cout<<char(i+'A')<<" = "<<arr[i]<<endl;
    }
    return 0;
}

UVALive 3755 Being Smarty!

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string reverse (string s)
{
    string result="";
    int sl=s.length();
    for (int i=0; i<sl; i++)
        result = s[i] + result ;
return result;
}

int main()
{
    int tc,kk=1, n, r, sl;
    string s;

    while(cin>>r && r)
    {
        cin>>n;
        getline(cin,s);
        sl=s.length();
        int pos=0;
        while(s[pos]==' ') pos++;
        
        string s1="";
        string s2="";
        if(s[pos]=='"')
        {
            pos++;
            while(s[pos]!='"')
            {
                s1+=s[pos];
                pos++;
            }
        }
        else
        {
            while(s[pos]!=' ')
            {
                s1+=s[pos];
                pos++;
            }
        }
        pos=sl-1;
        while(s[pos]==' ') pos--;
        if(s[pos]=='"')
        {
            pos--;
            while(s[pos]!='"')
            {
                s2+=s[pos];
                pos--;
            }
        }
        else
        {
            while(s[pos]!=' ')
            {
                s2+=s[pos];
                pos--;
            }
        }
        s2=reverse(s2);
      
        for(int i=0;i<s1.length();i++)
            if(s1[i]>='A' && s1[i]<='Z')
                s1[i]=tolower(s1[i]);
        for(int i=0;i<s2.length();i++)
            if(s2[i]>='A' && s2[i]<='Z')
                s2[i]=tolower(s2[i]);
        cout<<kk++<<". ";
        if(((r-1)/n)%2==0)
            cout<<s1<<endl;
        else cout<<s2<<endl;

    }
    return 0;
}

Friday 14 August 2015

UVALive 2052 Number Steps (ZOJ 1414, HDU 1391, POJ 1663, SPOJ NSTEPS, Regionals 2000 >> Asia - Tehran)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int arr[5005][5005];

void precal(int x, int y, int val)
{
    if(x>=502 && y>=502) return;
    arr[x][y]=val;
    //right-up
    x++, y++, val++;
    arr[x][y]=val;
    //right-down
    x++, y--, val++;
    arr[x][y]=val;
    //right-up
    x++, y++, val++;
    arr[x][y]=val;
    //left-up
    x--, y++, val++;
    precal(x, y, val);
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, m, x, y;
    string s;
    char ch;
    precal(0, 0, 0);
    cin>>tc;
    while(tc--)
    {
        cin>>x>>y;
        if(!x && !y)        cout<<0<<"\n";
        else if(arr[x][y])  cout<<arr[x][y]<<"\n";
        else                cout<<"No Number\n";
    }
return 0;
}

Tuesday 11 August 2015

UVA 846 Steps (POJ - 2590, ZOJ - 1871)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    long long lo, hi, dif, limit;
    string s;
    char ch;
    cin>>tc;
    while(tc--)
    {
        cin>>lo>>hi;
        dif=hi-lo;
        int step=0, inc=1;
        limit=0;
        while(dif>limit)
        {
            limit+=inc;
            step++;
            if(dif<=limit)
                break;
            limit+=inc;
            step++;
            inc++;
        }
        cout<< step <<"\n";
    }
return 0;
}