298A - Snow Footprints - CodeForces Solution


greedy implementation *1300

Please click on ads to support us..

Python Code:

n = int(input())
seq = input()

rBegin = rEnd = lBegin = lEnd = 0
R = L = False

for i in range(n):
    if seq[i] == 'R':
        if R == False:
            R = True
            rBegin = i+1
        rEnd = i+1
    elif seq[i] == 'L':
        if L == False:
            L = True
            lBegin = i+1
        lEnd = i+1

if R==True and L==True:
    print(rBegin, lBegin-1)
elif R==True:
    print(rBegin, rEnd+1)
else:
    print(lEnd, lBegin-1)

C++ Code:

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>

#define test_case int t;cin>>t;while(t--)
#define ll long long
#define endl "\n"
#define all(v) v.begin(),v.end()

using namespace std;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

string base_converter(ll x,ll y)
{
    char buffer [33];
    itoa (x,buffer,y);
    string s=buffer;
    return s;
}
string findSum(string str1, string str2)
{
    // Before proceeding further, make sure length
    // of str2 is larger.
    if (str1.length() > str2.length())
        swap(str1, str2);

    // Take an empty string for storing result
    string str = "";

    // Calculate length of both string
    int n1 = str1.length(), n2 = str2.length();

    // Reverse both of strings
    reverse(str1.begin(), str1.end());
    reverse(str2.begin(), str2.end());

    int carry = 0;
    for (int i=0; i<n1; i++)
    {
        // Do school mathematics, compute sum of
        // current digits and carry
        int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');

        // Calculate carry for next step
        carry = sum/10;
    }

    // Add remaining digits of larger number
    for (int i=n1; i<n2; i++)
    {
        int sum = ((str2[i]-'0')+carry);
        str.push_back(sum%10 + '0');
        carry = sum/10;
    }

    // Add remaining carry
    if (carry)
        str.push_back(carry+'0');

    // reverse resultant string
    reverse(str.begin(), str.end());

    return str;
}

long long gcd(long long a, long long b) {
    return a ? gcd(b % a, a) : b;
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b;
}

long long fact(long long n) {
    long long f = 1;
    for (int i = 2; i <= n; i++)
    {
        f *= i;
    }
    return f;
}
long long ncr(long long n, long long r) {
    return fact(n) / (fact(r) * fact(n - r));
}
long long npr(long long n, long long r) {
    return fact(n) / fact(n - r);
}

bool prime (long long x)
{
    long long i=2;
    bool ch=true;
    if (x==1)
        ch=false;
    while (x>=i*i)
    {
        if (x%i==0)
        {
            ch=false;
            break;
        }
        i++;
    }
    if (ch==false)
    {
        return false;
    }
    else
        return true;
}


int main()
{
int f=0 , l = 0;
    int size ; cin >> size ;
    string str ; cin >> str ;
    for(int i=0 ; i<size ;i++) {
        if (str[i] != '.') {
            f = i;
            break;
        }
    }
    for(int i=size-1 ; i>=0 ;i--) {
        if (str[i] != '.') {
            l = i;
            break;
        }
    }

    if(str[f]==str[l] and str[l]=='R')
        cout << f+1 << " " << l+1+1 ;
    else if(str[f]==str[l] and str[l]=='L')
        cout << l+1 << " " << f ;
    else
    {
        for(int i=f ; i<size ; i++)
        {
            l = i ;
            if(str[i]=='L')
                break;
        }
        cout << ++f << " " << l ;
    }
}


Comments

Submit
0 Comments
More Questions

1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent