Thursday, May 7, 2015

1012 - Guilty Prince (Light OJ)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<queue>
  5. #include<cstring>
  6. using namespace std;
  7. typedef pair<int,int> pii;
  8. #define pf printf
  9. #define sf scanf
  10. #define MAX 1000
  11.  
  12. char grid[50][50];
  13. bool vis[50][50];
  14. queue <pii> Q;
  15. int r , c;
  16. int dx[] = {00-11};
  17. int dy[] = {-1100};
  18.  
  19. bool valid(int x,int y)
  20. {
  21.     return x>=0 && y>=0 && x<&& y<&& (grid[x][y]=='.');
  22. }
  23. int bfs(int x, int y)
  24. {
  25.     int ux, uy, vx, vy, cnt=0;
  26.     memset(vis, 0sizeof(vis));
  27.     while(!Q.empty()) Q.pop(); //queue clear
  28.  
  29.     vis[x][y] =  true;
  30.     Q.push(pii(x,y));
  31.  
  32.     while(!Q.empty())
  33.     {
  34.         pii u = Q.front(); Q.pop();
  35.         ux = u.first; uy = u.second;
  36.         for(int i=0; i<4 ;i++)
  37.         {
  38.             vx = ux + dx[i];
  39.             vy = uy + dy[i];
  40.             if(!valid(vx, vy)) continue;
  41.             if(vis[vx][vy]) continue;
  42.             cnt++;
  43.             vis[vx][vy] = true;
  44.             Q.push(make_pair(vx, vy));
  45.         }
  46.     }
  47.     return cnt;
  48. }
  49. int main()
  50. {
  51.     //freopen("in.txt", "r", stdin);
  52.     int sr, sc, kase=0;
  53.     int test, t;
  54.     int i, j;
  55.     sf("%d"&test);
  56.     while(test--)
  57.     {
  58.         sf("%d%d"&c, &r);
  59.         for(i=0; i<r; i++)
  60.             sf("%s", grid[i]);
  61.  
  62.         for(i=0; i<r; i++)
  63.             for(j=0; j<c; j++)
  64.                 if(grid[i][j]=='@')
  65.                     sr = i , sc = j, i = j = c+r;
  66.  
  67.         int ans =  bfs(sr, sc)+1;
  68.         pf("Case %d: %d\n"++kase, ans);
  69.     }
  70.     return 0;
  71. }
  72.  

493B - Vasya and Wrestling (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define pf printf
#define sf scanf
#define loop(i, n) for(i=0; i<(n); i++)
#define MAX 10000
typedef long long ll;
vector<int> a, b;

int main()
{
    ll n, m, sum1, sum2, i, sz, j;
    int last, cnt , ans ;
    while(cin >> n)
    {
        ans =0;
        cnt=0;
        sum1 = sum2 = 0;
        a.clear();
        b.clear();
        loop(j,n)
        {
            cin >> m;
            if(j==(n-1)) last = m;
            if(m>0)
            {
                a.push_back(m);
                sum1 +=m;
            }
            else
            {
                m *=(-1);
                b.push_back(m);
                sum2 += m;
            }
        }
        sz=0;
        //cout << sum1 << " " << sum2 << endl;
        if(sum1>sum2) ans = 1;
        else if(sum2>sum1) ans = 2;
        else
        {
            if(a.size()> b.size()) sz = b.size();
            else                   sz = a.size();
            loop(i, sz)
            {
                if(a[i]>b[i])
                {
                    ans = 1;;
                    cnt++;
                    break;
                }
                else if(a[i]<b[i])
                {
                    ans = 2;
                    cnt++;
                    break;
                }
            }
            if(cnt==0)
            {
                if(last>0) ans = 1;
                else       ans = 2;
            }
        }
        if(ans==1) cout << "first" << endl;
        else if(ans==2) cout << "second" << endl;
    }
    return 0;
}

496B - Secret Combination (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define pf printf
#define sf scanf
#define loop(i, n) for(i=0; i<(n); i++)
#define MAX 100

int n;
string s, s1,  minS;

bool min(string s1, string s2)
{
    int i;
    int check = false;
    loop(i, n)
    {
        if(s1[i]==s2[i]) continue;
        else if(s1[i]>s2[i])
        {
            check = true;
            break;
        }
        else
        {
            check = false;
            break;
        }
    }
    return check;
}
void addOne()
{
    int i;
    loop(i, n)
    {
        if(s1[i]=='9')
            s1[i] = '0';
        else
            s1[i]++;
    }
}
void rightShift()
{
    s = s.substr(n-1,1)+ s.substr(0,n-1);
}
int main()
{
    int i, j;
    cin >> n;
    cin >> s;
    minS = s;
    loop(i, n)
    {
       s1 = s;
       loop(j, 9)
       {
            addOne();
            if(min(minS, s1))
                minS = s1;
       }
       rightShift();
       if(min(minS, s))
            minS = s;
    }
    cout << minS << endl;
    return 0;
}

499B - Lecture (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define pf printf
#define sf scanf
#define loop(i, n) for(i=0; i<(n); i++)
#define MAX 100
map<string, string> mp;

int main()
{
    //freopen("in.txt","r",stdin);
    int n, m;
    string s1, s2;
    cin >> n >> m;
    while(m--)
    {
        cin >> s1 >> s2;
        if(s1.length()>s2.length())
            mp[s1] = s2;
        else
            mp[s1] = s1;
    }
    while(n--)
    {
        cin >> s1;
        cout << mp[s1] << " ";
    }
    cout << endl;
    return 0;
}

508B - Anton and currency you all know (Codeforces)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define pf printf
#define sf scanf
#define loop(i, n) for(i=0; i<(n); i++)
#define MAX 100

int main()
{
    string s;
    int i, len;
    int index, temp, odd;
    int flag=0;
    cin >> s;
    len = s.length();
    odd = s[len-1]-'0';
    for(i=len-1; i>=0; i--)
    {
        temp = s[i]-'0';
        if(temp%2==0)
        {
            flag++;
            if(flag==1)
            {
                index = i;
            }
            if(odd>temp)
            {
                index = i;
            }
        }
    }
    if(flag==0)
        cout <<"-1" << endl;
    else
    {
        swap(s[index], s[len-1]);
        cout << s << endl;
    }
    return 0;
}

Friday, May 1, 2015

465A - inc ARG

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define sf scanf
#define pf printf
#define loop(i, n) for(i=0; i<(n) ; i++)
#define MAX 100
int main()
{
    string s;
    int n, i;
    cin >> n;
    cin >> s;
    int c = 1;
    int count = 0;
    loop(i, n)
    {
        if(s[i]=='0' && c==1)
        {
            count++;
            c = 0;
        }
        else if(s[i]=='1' && c==1)
        {
            count++;
            c = 1;
        }
        else
            break;
    }
    cout << count << endl;
    return 0;
}

Compare equality of two string in C

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