Problem - D - Codeforces
a a b a c a d b b c b d c c d d ( k = 4 ) aabacad~bbcbd~ccd~d(k = 4) aabacad bbcbd ccd d(k=4)
a a b a c a d a e b b c b d b e c c d c e d d e e ( k = 5 ) aabacadae~bbcbdbe~ccdce~dde~e(k = 5) aabacadae bbcbdbe ccdce dde e(k=5)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , m;
string ans;
signed main(){
cin >> n >> m;
if(m == 1) {
ans = 'a';
} else {
for(int i = 1 ; i <= m - 1 ; i ++) {
for(int j = i ; j <= m ; j ++) {
if(j != m) {
ans += char ('a' - 1 + j);
ans += char ('a' - 1 + i);
} else {
ans += char ('a' - 1 + j);
}
}
}
ans += char('a' - 1 + m);
}
// cout << ans << "\n";
string now;
if(ans.size() > n) {
ans.resize(n);
} else {
int cnt = ((n - 1) / (int)ans.size()) + 1;
for(int i = 1 ; i <= cnt ; i ++) now += ans;
now.resize(n);
ans = now;
}
cout << ans ;
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
Problem - E2 - Codeforces
复杂度 O ( n l o g n ) 复杂度O(nlogn) 复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
#define ull unsigned long long
const int Base = 131;
ull base[N] , h[N];
inline void init_hash(string s){
int n = s.size();
h[0] = 0;
for(int i = 1 ; i <= n ; i ++) h[i] = h[i - 1] * Base + s[i - 1];
base[0] = 1;
for(int i = 1 ; i <= n ; i ++) base[i] = base[i - 1] * Base;
}
inline ull get(int l,int r){
return h[r] - h[l] * base[r - l];
}
int n , m , id = -1;
string s , ans , res , nxt;
signed main(){
cin >> n >> m >> s;
ans = s;
s += s;
n += n;
init_hash(s);
s = '?' + s;
for(int i = 1 ; i <= n / 2 ; i ++) {
int l = 0 , r = i , mid = 0;
while(l < r) {
mid = (l + r) >> 1;
if(get(0 , mid + 1) != get(i , i + mid + 1)) r = mid;
else l = mid + 1;
}
int pos = l;
if(pos < i && s[pos + 1] < s[i + pos + 1]) {
id = i; break;
}
}
if(id != -1) ans = s.substr(1 , id);
int cnt = (m - 1) / ((int)ans.size()) + 1;
for(int i = 1 ; i <= cnt ; i ++) {
res += ans;
}
res.resize(m);
cout << res << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
/*
10 11
yyynhtexfv
8 17
dbcadabc
*/
时间复杂度 O ( n ) 时间复杂度O(n) 时间复杂度O(n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , m;
string s , ans , res , nxt;
int z[N] , id = -1;
void get_z(string s){
int n = s.size();
s = ' ' + s;
z[1] = n;
for(int i = 2 , l = 1 , r = 0 ; i <= n ; i ++){
if(i <= r) z[i] = min(z[i - l + 1] , r - i + 1);
else z[i] = 0;
while(s[z[i] + 1] == s[i + z[i]]) z[i] += 1;
if(i + z[i] - 1 > r) l = i , r = i + z[i] - 1;
}
}
signed main(){
cin >> n >> m >> s;
ans = s;s += s;n += n;
get_z(s);
s = ' ' + s;
for(int i = 1 ; i <= n / 2 ; i ++) {
int pos = z[i + 1];
if(pos < i && s[pos + 1] < s[i + pos + 1]) {
id = i; break;
}
}
if(id != -1) ans = s.substr(1 , id);
int cnt = (m - 1) / ((int)ans.size()) + 1;
for(int i = 1 ; i <= cnt ; i ++) {
res += ans;
}
res.resize(m);
cout << res << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
/*
10 11
yyynhtexfv
8 17
dbcadabc
*/
Problem - C - Codeforces
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int k , n;
string s;
int pre[N] , nex[N] , res;
signed main(){
IOS
cin >> k >> s;
n = s.size();
s = '?' + s;
vector<int>ve;
int cnt = 0;
for(int i = 1 ; i <= n ; i ++) {
if(s[i] == '1') {
ve.push_back(cnt);
ve.push_back(-1);
cnt = 0;
} else {
cnt += 1;
if(i == n) ve.push_back(cnt);
}
}
if(ve.back() == -1) ve.push_back(0);
n = ve.size();
cnt = 0;
if(k == 0) {
for(int i = 0 ; i < n ; i ++) {
if(ve[i] != -1) res += (ve[i] + 1) * ve[i] / 2;
}
} else {
for(int i = 0 ; i < n ; i ++) {
if(ve[i] == -1) {
nex[++cnt] = ve[i + 1];
pre[cnt] = ve[i - 1];
}
}
if(cnt < k) {
res = 0;
} else {
for(int i = 1 ; i + k - 1 <= cnt ; i ++) {
res += (pre[i] + 1) * (nex[i + k - 1] + 1);
}
}
}
cout << res << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);