Showing posts with label POJ. Show all posts
Showing posts with label POJ. Show all posts

Saturday, 9 July 2016

DNA Sorting (UVA 612, HDU 1379, POJ 1007, ZOJ 1188, UVALive 5414, Regionals 1998 >> North America - East Central NA)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva), 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 kk=1, tc, n, m, cnt;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n>>m;
        vector< pair<int, pair<int, string> > > v;
        for(int i=0;i<m;i++)
        {
            cin>>s;
            cnt=0;
            for(int j=0;j<n-1;j++)
                for(int k=j+1;k<n;k++)
                    if(s[j]>s[k])
                        cnt++;
            v.push_back(make_pair(cnt, make_pair(i,s)));
        }
        sort(v.begin(), v.end());
        if(kk>1) cout<<"\n";
        for(int i=0;i<m;i++)
            cout<<v[i].second.second<<"\n";
        kk++;
    }

    return 0;
}

Sunday, 5 June 2016

Wooden Sticks (UVALive 2322, ZOJ 1025, HDU 1051, POJ 1065, SPOJ MSTICK, Regionals 2001 >> Asia - Taejon)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 100000


int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    pair<int, int> pii[5002];
    bool vis[5002];
    string s;
    char ch;
    cin>>tc;
    while(tc--)
    {
        CLR(vis);
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>pii[i].first>>pii[i].second;
        sort(pii, pii+n);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                cnt++;
                vis[i]=true;
                int cur=i;
                for(int j=i+1;j<n;j++)
                    if(!vis[j])
                        if(pii[j].second>=pii[cur].second)
                        {
                            vis[j]=true;
                            cur=j;
                        }
            }

        }
        cout<< cnt <<"\n";
    }
return 0;
}

Friday, 27 May 2016

Prime Path (UVA 12101, SPOJ PPATH, POJ 3126, HDU 1973, UVALive 3639, Regionals 2006 >> Europe - Northwestern)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 10000

vector<int>prime;
bool status[MX+2];
int pl;

void PrimeGenerate(int n)
{
    int sq=sqrt(n);
    for(int i=3; i<=sq; i+=2)
    {
        if(!status[i])
            for(int j=i*i; j<=n; j+=(i<<1))
                status[j]=true;
    }

    status[0]=status[1]=true;
    for(int i=4;i<=n;i+=2) status[i]=true;

    prime.push_back(2);
    for(int i=3; i<=n; i+=2)
        if(!status[i])
            prime.push_back(i);
return;
}

int dp[MX], st, ed, taken[MX], dist[10010];
vector<int>v[10010];

int bfs(int num)
{
    if(num==ed) return 1;
    dist[num]=1;
    queue<int>Q;
    Q.push(num);
    while(!Q.empty())
    {
        int qq=Q.front();
        Q.pop();
        for(int i=0;i<v[qq].size();i++)
            if(!dist[v[qq][i]])
            {
                dist[v[qq][i]]=dist[qq]+1;
                if(v[qq][i]==ed) return dist[v[qq][i]];
                Q.push(v[qq][i]);
            }
    }
return 100000;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    PrimeGenerate(MX);
    int loopst;

    pl=prime.size();
    for(int i=0;i<pl;i++)
        if(prime[i]>999)
        {
            loopst=i;
            break;
        }

    for(int i=loopst;i<pl;i++)
    {

        for(int j=loopst;j<pl;j++)
        {
            int tmp=prime[i];
            int tmp2=prime[j], cnt=0;

            while(tmp && tmp2)
            {
                if(tmp%10 != tmp2%10) cnt++;
                tmp/=10;
                tmp2/=10;
            }

            if(cnt==1)
            {
                v[prime[i]].push_back(prime[j]);
            }
        }
    }

    cin>>tc;
    while(tc--)
    {
        cin>>st>>ed;
        if(ed>st) swap(st, ed);
        CLR(dist);
        int ans=bfs(st);
        if(ans<100000) cout<<ans-1<<"\n";
        else cout<<"Impossible\n";
    }

return 0;
}

Wednesday, 2 March 2016

Word Index (UVA 417, UVALive 5392, ZOJ 1342, POJ 1496, HDU 1336, SCU 1024)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / 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 100000

map<string, int> mp;
int cnt;

void precal(string s, int len)
{
    if(s.length()==len)
    {
        mp[s]=cnt++;
        return;
    }
    //cout<<s<<endl;
    char lst;
    if(s.length()==0)
        lst='a';
    else 
        lst = s[s.length()-1]+1;
    for(char ch=lst;ch<='z';ch++)
        precal(s+ch, len);
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n;
    string s;
    cnt=1;
    for(int i=1;i<=5;i++)
    {
        precal("", i);
       // cout<<i<<endl;
    }
    while(cin>>s)
    {
        cout<<mp[s]<<"\n";
    }

return 0;
}