Contest Duration: - (local time) (150 minutes) Back to Home

Submission #7861204

Source Code Expand

Copy
```#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <string>
#include <queue>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#define ll long long
#define fr(i,k,N) for(decltype(N+k) i=k; i<N;i++)
#define rep(i,N) for(decltype(N) i=0; i<N;i++)
#define ALL(v) v.begin(),v.end()
using namespace std;
template <class X> void pr(X test){cout<<test<<endl;}
template <class X> void prr(X test){for(auto it:test){cout<<it<<endl;}}
template <class X> void prrr(X test){int f=0;for(auto it:test){cout<<(f++!=0?" ":"")<<it;}cout<<endl;}
template<typename X>
istream&operator>>(istream&i,vector<X>&v){for(X&x:v)i>>x;return i;}

class UF { // union find from https://github.com/kartikkukreja/blog-codes (MIT)
// [usage]
// auto uf = new UF(n);
// uf->merge(l, r); // 0 <= l, r <= n-1
// ...
public:
int *id; // inner id
int cnt; // number of all vartices
int *sz; // sz[i] means size of i's set
UF(int N)   {
cnt = N;
id = new int[N];
sz = new int[N];
for(int i=0; i<N; i++) {
id[i] = i;
sz[i] = 1;
}
}
~UF() {
delete [] id;
delete [] sz;
}
int find(int p) {
int root = p;
while (root != id[root]) root = id[root];
while (p != root) {
int newp = id[p];
id[p] = root;
p = newp;
}
return root;
}
void merge(int x, int y) {
int i = find(x);
int j = find(y);
if (i == j) return;

if (sz[i] < sz[j]) {
id[i] = j;
sz[j] += sz[i];
} else {
id[j] = i;
sz[i] += sz[j];
}
cnt--;
}
bool connected(int x, int y) {
return find(x) == find(y);
}
int count() {
return cnt;
}
};

// return 約数unordered_set 1 <= x <= n
auto divv = [](auto n) {
unordered_set<decltype(n)> v;
for(decltype(n) i = 1; i*i <= n; i++) {
if(n%i == 0) {
v.insert(i);
if(i*i != n) v.insert(n/i);
}
}
return v;
};

// check prime (and n>=2) or not
auto isPrime = [](auto n){
if(n <= 1) return false;
for(decltype(n) i = 2; i <= sqrt(n); ++i) {
if(n % i == 0) {
return false;
}
}
return true;
};

int main() {
ll k;
string s;
cin>>s>>k;
ll f = s[0];
ll g  = true;
for(auto it:s) if(it!=f)g=false;
if(g) {
pr(s.size()*k/2);
}
else if(s[0] != s[s.size()-1]){
ll lngth = 0;
ll sum = 0;
for(ll i =0;i<s.size();i++){
if(i == s.size()-1){
sum+=(lngth+1)/2;
}
else if(s[i] == s[i+1]){
lngth+=1;
}else{
sum+=(lngth+1)/2;
lngth=0;
}
}
pr(sum*k);
} else {
auto s2 = s+s;
ll sum2 = 0;
{
ll lngth = 0;
for(ll i =0;i<s2.size();i++){
if(i == s2.size()-1){
sum2+=(lngth+1)/2;
}
else if(s2[i] == s2[i+1]){
lngth+=1;
}else{
sum2+=(lngth+1)/2;
lngth=0;
}
}
}
ll sum =0;
{
ll lngth = 0;
for(ll i =0;i<s.size();i++){
if(i == s.size()-1){
sum+=(lngth+1)/2;
}
else if(s[i] == s[i+1]){
lngth+=1;
}else{
sum+=(lngth+1)/2;
lngth=0;
}
}
}
ll diff = sum2 - sum;
pr(sum + (k-1)*diff);
}
}
```

#### Submission Info

Submission Time 2019-10-05 21:20:46+0900 A - Connection and Disconnection quiye C++14 (GCC 5.4.1) 300 4085 Byte AC 1 ms 256 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
 AC × 3
 AC × 15
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 1 ms 256 KB
02.txt AC 1 ms 256 KB
03.txt AC 1 ms 256 KB
04.txt AC 1 ms 256 KB
05.txt AC 1 ms 256 KB
06.txt AC 1 ms 256 KB
07.txt AC 1 ms 256 KB
08.txt AC 1 ms 256 KB
09.txt AC 1 ms 256 KB
10.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB
s3.txt AC 1 ms 256 KB