645E - Intellectual Inquiry - CodeForces Solution


dp greedy strings *2200

Please click on ads to support us..

C++ Code:

// author: gary

// created: 2018/04/21 15:28:31

#include <cassert>

#include <cctype>

#include <climits>

#include <cmath>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <ctime>

#include <limits>

#include <utility>

#include <functional>

#include <string>

#include <bitset>

#include <deque>

#include <list>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <vector>

#include <algorithm>

#include <iostream>

#include <sstream>

using namespace std;

#define SZ(x) ( (int) (x).size() )

#define ALL(c) (c).begin(), (c).end()

#define CNT(c, x) ((c).find(x) != (c).end())

typedef pair<int, int> pii;

typedef long long ll;

template<class T> bool cmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }

template<class T> bool cmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }

const int N = 1e6 + 10;

const int mod = 1e9 + 7;

void madd(int& a, int b) {

  a += b;

  if(a >= mod)

    a -= mod;

}



int mul(int a, int b) {

  return (1LL * a * b) % mod;

}



int add(int a, int b) {

  return (a + b) % mod;

}



int sub(int a, int b) {

  return (a - b + mod) % mod;

}



int mpow(int a, int n) {

  int r = 1;

  while(n > 0) {

    if(n & 1)

      r = mul(r, a);

    a = mul(a, a);

    n >>= 1;

  }

  return r;

}



int n, m, sigma, tot;

int last[N], L[N], p[N];

char s[N];



int main() {

  scanf("%d%d%s", &n, &sigma, s);

  m = strlen(s);



  tot = 1;

  memset(last, -1, sizeof last);

  for(int i = 0; i < m; i++) {

    int c = s[i] - 'a';

    int tmp = sub(tot, L[c]);

    L[c] = tot;

    madd(tot, tmp);

    last[c] = i;

  }



  for(int i = 0; i < sigma; i++) {

    p[i] = i;

  }

  auto byLast = [&] (int i, int j) -> bool {

    return last[i] < last[j];

  };

  sort(p, p + sigma, byLast);



  for(int i = 0; i < n; i++) {

    int j = p[i % sigma];

    int tmp = sub(tot, L[j]);

    L[j] = tot;

    madd(tot, tmp);

  }



  printf("%d\n", tot);

  return 0;

}


Comments

Submit
0 Comments
More Questions

519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM
118D - Caesar's Legions
1598A - Computer Game
1605A - AM Deviation
1461A - String Generation
1585B - Array Eversion
1661C - Water the Trees
1459A - Red-Blue Shuffle
1661B - Getting Zero
1661A - Array Balancing
1649B - Game of Ball Passing
572A - Arrays
1455A - Strange Functions
1566B - MIN-MEX Cut
678C - Joty and Chocolate