问题

https://vjudge.net/problem/HackerRank-beautiful-string

给一个字符串S,可以任意取走S中的两个字符从而得到另外一个字符串P,求有多少种不同的P

\(\left|S\right|\le10^6\)

题解

想到组合数
将连续的相同的字符分为1组,如字符串“aaabbbbaccca
则可以选两组中的两个字母或一组中的两个字母
计算出连续的组数设为x
计算出长度大于等于2的组数设为y
\[ans = \left( \begin{array}{l}x\\2\end{array} \right) + y\]
时间复杂度$O(n)$

这样当然WA了

因为没有考虑是否存在两种移除(单个)字母的方法使结果相同。:(

以下考虑粗体的字母,且每种情况中xxxxx都不会造成前面情况

1.容易得aba,移除前两个字母和移除后两个字母效果相同。

2.容易得abc,(a$\ne$bb$\ne$ca$\ne$c),任意移除两个都不会得到相同的P

3.容易得abxxxxxca$\ne$b,|xxxxx|>0)和axxxxxbcb$\ne$c,|xxxxx|>0),任意移除两个都不会得到相同的P

4.容易得axxxxxbxxxxxc,无论abc相同还是不同,任意移除两个都不会得到相同的P

5.字符串A $\ne$ B,那么xxxxxAxxxxx$\ne$xxxxxBxxxxx

所以移除单个字母只会因为第一种情况造成重复

多个aba重复可以减去后面的情况,因此我们可以直接减去aba出现的次数

AC代码

#include <bits/stdc++.h>
#define REP(i,x,y) for(register int i=x; i<y; i++)
#ifdef LOCAL
#define DBG(x,...) printf(x, ##__VA_ARGS__)
#else
#define DBG(x,...) (void)(0)
#endif
using namespace std;
char S[1000007];
inline void gs(int &p) {
p=0;
while((S[p++]=getchar())>' ');
S[--p]=0;
}
int main()
{
int r;gs(r);
long long x=0,y=0;
char l=0,cn=1;
REP(i,0,r) {
if(S[i]!=l) {
x++;
if(cn>=2) y++;
cn=1;
} else {
cn++;
}
l=S[i];
}
if(cn>=2) y++,cn=1;
// DBG("%s %d %lld %lld\n", S, r, x, y);
long long ans= (x&1) ? (x-1)/2*x+y : x/2*(x-1)+y;
// long long t=0;
r--;
REP(i,1,r) {
if(S[i-1]==S[i+1] && S[i-1]!=S[i]) ans--;
}
printf("%lld\n", ans);
return 0;
}

HackerRank beautiful string的更多相关文章

  1. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  2. hiho一下:Beautiful String

    hiho一下:Beautiful String 记不清这是 hiho一下第几周的题目了,题目不难,不过对于练习编程,训练思维很有帮助.况且当时笔者处于学习算法的早期, 所以也希望刚接触算法的同学能多去 ...

  3. CF1328B K-th Beautiful String

    CF1328B K-th Beautiful String,然而CF今天却上不去了,这是洛谷的链接 题意 一个长度为\(n\)的字符串,有2个\(\texttt{b}\)和\(n-2\)个\(\tex ...

  4. hihocoder 1061.Beautiful String

    题目链接:http://hihocoder.com/problemset/problem/1061 题目意思:给出一个不超过10MB长度的字符串,判断是否里面含有一个beautiful strings ...

  5. Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...

  6. B. Pasha and String

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>

    L - Ferris Wheel String Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 43000/43000KB (Java/ ...

  8. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  9. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

随机推荐

  1. django url之path默认参数

    url path指向视图创建和更新数据 实例: from django.urls import path from . import views urlpatterns = [ path('blog/ ...

  2. [书籍]重温《Framework Design Guidelines》

    1. 前言 最近重温了<Framework Design Guidelines>. <Framework Design Guidelines>中文名称为<.NET设计规范 ...

  3. spark 2.3 导致driver OOM的一个SparkPlanGraphWrapper源码的bug

    背景 长话短说,我们部门一个同事找到我,说他的spark 2.3 structured streaming程序频繁报OOM,从来没有坚持过超过三四天的,叫帮看一下. 这种事情一般我是不愿意看的,因为大 ...

  4. Django Restframework 过滤器

    一.基本配置: 1.安装:pip install django-filter 2.将 django_filters 配置到INSTALLED-APPS中 3.对 REST_FRAMEWORK 配置: ...

  5. 配置linux-Fedora系统下iptables防火墙

    参考地址:https://blog.csdn.net/zhangjingyi111/article/details/78902820 本篇文章为实验课过程记录,较为简略. 1.查看系统是否安装ipta ...

  6. elasticsearch(6.2.3)安装Head插件

    一.安装elasticsearch,参照:https://www.cnblogs.com/dyh004/p/8872443.html 二.安装nodejs,参照:https://www.runoob. ...

  7. 我的微信小程序第一篇(入门)

    前言 微信小程序出来已经有一段时间了,已经有很多大牛都写过相关教程了,那么我为啥还要写这篇文章呢?其实仅仅是个人对微信开发的一个兴趣吧,觉得是个很有意思的事情,后面有时间可能会发更多关于小程序的文章, ...

  8. Python-random模块-59

    random模块: 随机数模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.76643 ...

  9. Python-爬虫小例子-55

    import re from urllib.request import urlopen def getPage(url): response = urlopen(url) return respon ...

  10. c语言之sizeof的细节

    关于sizeof,我们知道sizeof并不是一个函数,尽管通常我们会用sizeof()用法(这是c语言的坑),在此关于sizeof的一些关键不被认知的进行一下总结: # include "i ...