1029A - Many Equal Substrings - CodeForces Solution


implementation strings *1300

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
t = input()
x = 0
for i in range(1, n):
  if t[:i] == t[-i:]:
    x = i
t += t[x:] * (k - 1)
print(t)

C++ Code:

#include <bits/stdc++.h>

using namespace std ;

typedef  long long  ll ;
#define  test int t;cin>>t;while(t--)
#define fast() {ios::sync_with_stdio(0) ; cin.tie(0); cout.tie(0);}
int mod = 1e9 + 7;
// int n, a[16][16];
// ll dp[16][1<<16];
// ll solve(int i, int mask)
// {
//   // cout << i << ' ' << mask << endl;
//     if(i == n) return 0;
//     ll &ret = dp[i][mask];
//     if(~ret) return ret;
//     ll sum = 0;
//     for(int j = 0; j < i; j++){
//         if(mask & (1 << j)) sum += a[j][i];
//     }
//     ll path1 = 0, path2 = 0;
//     path1 += solve(i + 1, mask | (1 << i)) + sum;
//     path2 += solve(i + 1, mask);
//     return ret = max(path1, path2);
// }

int main()
{
    fast();
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    string a = "", b;
    int j = -1;
    for(int i = 0; i < n - 1; i++){
        a += s[i];
        b = s.substr(n - i - 1, i + 1);
        if(a == b) j = i;
    }
    a = s.substr(j + 1, n - (j + 1));
    cout << s;
    while(k-- > 1) cout << a;
    return 0;
}


Comments

Submit
0 Comments
More Questions

169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String
1708B - Difference of GCDs
863A - Quasi-palindrome
1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph