Thursday, May 7, 2015

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;
}

No comments:

Post a Comment

Compare equality of two string in C

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