brute force combinatorics dp strings

Please click on ads to support us..

C++ Code:

// hi its me ALT F4
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 using ld = long double;
typedef vector<ll>vl;
typedef deque<ll>dq;
typedef pair<ll,ll>pl;
#define endl '\n'
#define all(a) (a).begin(),(a).end()
 #define allr(a) (a).rbegin(),(a).rend()
#define fi first
 #define se second
#define mp make_pair
 #define mt make_tuple
#define pb push_back
#define eb emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
#define no cout<<"NO"<<"\n";
#define yes cout<<"YES"<<"\n";
 const ll MOD1 = 1e9+7;
 const ll MOD9 = 998244353;
 const ll INF = 1e18;
 const ld pi = 3.1415926535897932384626433832795;
 int  gcd(int a, int  b)
    {
        if (a == 0 || b == 0)
            return 0;
        while (b != 0)
        {
            int  tmp;
            tmp = a % b;
            a = b;
            b = tmp;
        }
        return a;
    }

  ll binexp(ll a, ll k,ll mod=MOD1){
    ll ans = 1;
    while (k){
        if (k & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        k >>= 1;
    }
    return ans;
  }




 string to_bin(ll n){
    string ch="";
    ll i = 0;
    while (n>0) {
        char a=n%2+'0';
        ch=a+ch;
        n = n / 2;
        i++;
    }
    return ch;
}




ll to_dec(string ch){
    ll res=0;
    for (ll i=ch.size()-1;i>=0;i--){
        ll a=ch[i]-'0';
        ll p=ch.size()-i-1;
        if(a)res+=binexp(2,p,MOD1);
        //DONT'T FORGET THE MOD
    }
    return res;
}





ll C(ll n,ll r){
    ll p=1,k=1;
    if(r>n)return 0;
    if (n-r<r)r=n-r;
    if(r!=0){
            while(r){
                p*=n;
                k*=r;
                ll m =__gcd(p,k);
                p/=m;
                k/=m;
                n--;
                r--;

                }
            }
    return p;
}





 bool estentier(long double nombre) {
    ll entier = (ll)nombre;
    return (nombre - entier == 0);
}

ll estpremier(ll nombre) {
    if (nombre < 2) return false;
    for (int i = 2; i<=sqrt(nombre); i++) {
        if (nombre % i == 0) return false;
    }
    return 1;
}





ll sum_digit(ll n){
     ll s=0;
     while(n/10!=0){
         s+=(n%10)*(n%10);
         n=n/10;
     }
     s+=n*n;
     return s;
 }



 ll factoriel(ll N)
{
if(N<=1)
return 1;
else
return(N*factoriel(N-1))%MOD9;
}




 bool comparePairs(const std::pair<ll, ll>& pair1, const std::pair<ll, ll>& pair2) {
    if (pair1.first != pair2.first) {
        return pair1.fi > pair2.fi;  // Sort by the first element in ascending order
    }
    else {
        return pair1.se < pair2.se;  // Sort by the second element in ascending order
    }
}
 bool comparePairs2(const std::pair<ll, ll>& pair1, const std::pair<ll, ll>& pair2) {
    if (pair1.first != pair2.first) {
        return pair1.first > pair2.first;  // Sort by the first element in ascending order
    }
    else {
        return pair1.second < pair2.second;  // Sort by the second element in ascending order
    }
}





ll ppcm(ll X,ll Y)
{
  ll A=X;
  ll B=Y;
  while (A!=B)
  {
    while (A>B) B=B+Y;
    while (A<B) A=A+X;
  }
  return A;
}





ll maxSubArraySum(ll a[], ll n)
{
    int max_so_far = INT_MIN, max_ending_here = 0;

    for (int i = 0; i < n; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}





/*bool isPermutation(vector<ll> a,ll n) {
   for (int i = 0; i < n; ++i) {
       if (a[i] <= 0 || a[i] > n) {
            return false;
        }
  }
    set<ll> s(a.begin(), a.end());
    return (s.size()== n);
}*/




ll combinaison(ll n, ll p) {
    if(p == 0 || p == n)
        return 1;
    if(0 < p && p < n)
        return combinaison(n-1, p-1)%MOD9 + combinaison(n-1, p)%MOD9;
    return 0;
}


bool isPalindrome(const std::string &str) {
    std::string reversed = str;
    std::reverse(reversed.begin(), reversed.end());
    return str == reversed;
}



/*******************SIEVE & GRAPH & DIJEKSTRA**********************/
const int N=300001;
vector<bool> prim(N,true);

void sieve()
{
    rep(i,2,N)
    {
        if(prim[i])
        {
            for(int j=2;j*i<N;j++)prim[i*j]=false;
        }
    }
}



vector<vector<ll>> graph(N+1);

bool vis[N];
vector<bool> parent(N+1,false);
//vector<ll> leaf(N+1,1);
//vector<ll> dist(N+1);
ll c1=0,c2=0;

 void dfs(ll a)
    {
     vis[a]=true;
     parent[a]=true;

     c1++;



     for(auto e: graph[a])
     {

             dfs(e);


     }


 parent[a]=false;
 //if(a==2)cout<<"here"<<endl;

 }



 bool cycle=false;
 void detectcycle(ll a)
 {  if(cycle)return;
     parent[a]=true;
     for(auto e:graph[ a ])
     {
         if(parent[e]){cycle=true; break;}
     }
     parent[a]=false;
 }
/* void dijkistra(int node,int n)
{
    priority_queue<pl, vector<pl>, greater<pl>> pq;

    for (int i=0 ; i<n ; i++)
    {
        if (i == node)
            dist[i]=0;
        else
            dist[i] = INF;
    }

    pq.push({dist[node], node});

    while(!pq.empty())
    {
        pl parent = pq.top();
        pq.pop();

        int h = parent.first;
        ll index = parent.second;

        if (h>dist[index])
            continue;

        for (auto childPair:graph[index])
        {
            ll child = childPair.first;
            ll dFromParentToChild = childPair.second;
            if (dist[child]>dist[index]+dFromParentToChild)
            {
                dist[child] = dist[index]+dFromParentToChild;
                pq.push({dist[child], child});
            }

        }
    }
}
*/

long long calc(pair<long long, long long> a[], pair<long long, long long> b[], pair<long long, long long> c[], int n) {
    long long ans = 0;
    for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
            for (int l = 0; l < 3; l++) {
                if (a[j].second != b[k].second and a[j].second != c[l].second and b[k].second != c[l].second) {
                    ans = max(ans, a[j].first + b[k].first + c[l].first);
                }
            }
        }
    }
    return ans;
}
bool estOrdinaire(long long  nombre) {
    // Convertir le nombre en une chaîne de caractères
    std::string nombreStr = std::to_string(nombre);

    // Utiliser un ensemble pour stocker les chiffres uniques
    std::unordered_set<char> chiffres;

    // Parcourir chaque chiffre dans la chaîne
    for (char chiffre : nombreStr) {
        chiffres.insert(chiffre);
    }

    // Vérifier si tous les chiffres sont les mêmes
    return chiffres.size() == 1;
}
struct CompareCost {
    bool operator()(long long  a, long long  b) {
        return a < b;
    }
};
void solve() {
    long long  n;
    cin >> n;
    string s;
    cin>>s;
    set<char> sett;
    long long res =0;
    for(char e:s){
        sett.insert(e);
        res += sett.size();
    }
    cout<<res<<endl;

}

//lets start the game
int main()
{
    // Setting up input/output stream and precision.
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(15);
    int t;
    cin >>t;
    while (t--)
    {
        solve();
    }
    return 0;
}






Comments

Submit
0 Comments
More Questions

589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies