D2C104 - Editorial

PROBLEM LINK:

Contest

Author: Pratyaksh
Tester: Pankaj Sharma
Editorialist: Pratyaksh

DIFFICULTY:

Easy

PREREQUISITES:

String manipulation, Modular arithmetic

PROBLEM:

Cyclic left shift of given string x times followed by increasing each character value y times.

QUICK EXPLANATION:

  • x \lt |S| (|S| is length of string)
  • y \lt 26 (As there are 26 lowercase characters)

The above constraints can be achieved with modulus function (%).
Further S can be traversed from x(left shift) followed by remaining initial part.
Each character value will be increased y times taking care of out of range cases (increased character value exceeds z).

EXPLANATION:

In this problem, left shift is possible only when x \lt |S| as whenever x =|S|, effective left shift of string will be zero.
Similarly y \lt 26 as whenever y=26 effective increment in character value will be zero.
In case y \lt 0, we can increase its value by 26 as the result will be same no matter how many times we increment character value by 26.
Now to print left shifted string, we can simply traverse string from xth index and further traversing remaining characters.
While traversing string, we increase each character value by y followed by testing for out of range case.
In each out of range case i.e character value is greater than z, we decrement its value by 26 as increment character value after z will result in a.

SOLUTIONS:

Setter's Solution
    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        int t;
        cin>>t;
        while(t--){
            string s;
            int x,y;
            cin>>s>>x>>y;
            x%=s.length();
            y%=26;
            if(y<0)
                y+=26;
            for(int i=x; i<s.length(); i++){
                int temp=(int)s[i];
                temp+=y;
                if(temp>122)
                    temp-=26;
                cout<<(char)temp;
            }
            for(int i=0; i<x; i++){
                int temp=(int)s[i];
                temp+=y;
                if(temp>122)
                    temp-=26;
                cout<<(char)temp;
            }
            cout<<endl;
        }
    }

Tester's Solution
    #include<bits/stdc++.h>
    using namespace std;
	typedef long long           ll;
	#define all(x) (x).begin(), (x).end()
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; }
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args) {
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << " = " << arg1 << " | "; __f(comma + 1, args...);
	}
	void solve() {
		ll ans = 0;
		ll n;
		string s;
		cin >> s;
		ll x, y;
		cin >> x >> y;
		n = s.size();
	
		int max = 1e9;
		assert(n >= 1 and n <= 1000);
		assert((-max) <= x and x <= max);
		assert((-max) <= y and y <= max);
		string newone = s;
		x %= n;
		y %= 26;
		for (int i = 0; i < n; i++)
		{
			int newIndex = (i  - x + n) % n;;
			newone[newIndex] = 'a' + (s[i] - 'a' + y + 26) % 26;
		}
		cout << newone << "\n";
	}
	int main()
	{
		cin.sync_with_stdio(0);
		cin.tie(0);
		cout.tie(0);
		int t;
		
		cin >> t;
		int max = 1e5;
		assert(t >= 1 && t <= 1000);
		while (t--)
			solve();
		return 0;
	}

For doubts, please leave them in the comment section, I’ll address them.