AtCoder Grand Contest 019 B - Reverse and Compare

题意:给定字符串,可以选定任意i、j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符串,问能组成多少种不同的字符串。

  tourist出的题。。感觉好棒,虽然简单,但我就是不知道怎么弄,感觉思维好匮乏。

  首先,如果Si=Sj,那么反转i到j和翻转i+1到j-1是一样的,也就是这种翻转不会贡献更多的答案。那么其实只要求i<j且Si!=Sj的个数就行了,当然,本身不变也是一种答案。求解i<j且Si!=Sj的个数可以从总的i<j的翻转个数中减去不合法的,也就是Si==Sj的。总的个数就是字符串长度len*(len-1)/2,因为可以这样想,s[0]能与后面len-1个字符组成字串翻转,s[1]能跟后面len-2个字符组成字符串翻转...,所以总结果就是len-1+len-2+len-3+...+1=len*(len-1)/2。然后统计每个字符出现的次数cnt(c),每种字符组成的不合法种数是cnt(c)*(cnt(c)-1)/2,跟上面道理一样。所以答案就是:

          1+len*(len-1)/2-∑cnt(c)*(cnt(c)-1)/2

  看来数据类型尽量要统一,刚开始设len为int类型就WA了,全是long long就对了。

  

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
map<int, ll> mp;
char s[]; int main()
{
cin >> s;
ll len = strlen(s);
for (int i = ; i < len; i++)
mp[s[i] - 'a']++;
ll ans = ;
ans += len*(len - ) / ;
for (int i = ; i < ; i++)
if (mp[i]) ans -= mp[i] * (mp[i] - ) / ;
cout << ans << endl;
return ;
}

AtCoder Grand Contest 019 B - Reverse and Compare【思维】的更多相关文章

  1. AtCoder Grand Contest 019 B: Reverse and Compare

    题意: 给出一个字符串,你可以选择一个长度大于等于1的子串进行翻转,也可以什么都不做.只能翻转最多一次. 问所有不同的操作方式得到的字符串中有多少个是本质不同的. 分析 tourist的题妙妙啊. 首 ...

  2. AtCoder Grand Contest 019 F-yes or no

    AtCoder Grand Contest 019 F-yes or no 解题思路: 考虑一个贪心策略,假设当前还有 \(x\) 道 \(\text{yes}\) 和 \(y\) 道 \(\text ...

  3. AtCoder Grand Contest 019 A: Ice Tea Store

    tourist出的题诶!想想就很高明,老年选手可能做不太动.不过A题还是按照惯例放水的. AtCoder Grand Contest 019 A: Ice Tea Store 题意:买0.25L,0. ...

  4. AtCoder Grand Contest 019

    最近比较懒,写了俩题就跑了 A - Ice Tea Store 简化背包 #include<cstdio> #include<algorithm> using namespac ...

  5. AtCoder Grand Contest 019 题解

    传送门 \(A\) 咕咕 int a,b,c,d,n,t; int main(){ scanf("%d%d%d%d%d",&a,&b,&c,&d,& ...

  6. 【agc019f】AtCoder Grand Contest 019 F - Yes or No

    题意 有n个问题答案为YES,m个问题答案为NO. 你只知道剩下的问题的答案分布情况. 问回答完N+M个问题,最优策略下的期望正确数. 解法 首先确定最优策略, 对于\(n<m\)的情况,肯定回 ...

  7. AtCoder Grand Contest 032 A - Limited Insertion( 思维)

    Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement Snuke has an empty ...

  8. AtCoder Grand Contest 005

    AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...

  9. AtCoder Grand Contest 002

    AtCoder Grand Contest 002 A - Range Product 翻译 告诉你\(a,b\),求\(\prod_{i=a}^b i\)是正数还是负数还是零. 题解 什么鬼玩意. ...

随机推荐

  1. 使用netbeans 搭建maven工程 整合spring springmvc框架

    使用netbeans7.4 自带的tomcat7.0 所以jdk选择7.xx 然后等待生成空的工程,会缺一些文件夹,和文件,后续需要的时候补齐 然后修改pom.xml添加引用,直接覆盖dependen ...

  2. leetcode 76 dp& 强连通分量&并查集经典操作

    800. Similar RGB Color class Solution { int getn(int k){ return (k+8)/17; } string strd(int k){ char ...

  3. 2019.8.13 NOIP模拟测试19 反思总结

    最早写博客的一次∑ 听说等会儿还要考试[真就两天三考啊],教练催我们写博客… 大约是出题最友好的一次[虽然我还是炸了],并且数据也非常水…忽视第三题的锅的话的确可以这么说.但是T3数据出锅就是你的错了 ...

  4. java-继承进阶_抽象类_接口

    概要图 一, 继承的进阶 1.1,成员变量 重点明确原理. 特殊情况: 子父类中定义了一模一样的成员变量. 都存在于子类对象中. 如何在子类中直接访问同名的父类中的变量呢? 通过关键字 super来完 ...

  5. Python 文件读写小结

  6. 洛谷P1052 过河

    P1052 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上. 由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青 ...

  7. gawc全球城市

    http://www.lboro.ac.uk/gawc/world2016t.html Global city From Wikipedia, the free encyclopedia     Pa ...

  8. nested exception is org.hibernate.MappingException解决方案

    1.可能是因为映射文件( Order.hbm.xm)配置的class路径出错 <hibernate-mapping> <class name="com.web.bean.O ...

  9. mysql实现行拼接、列拼接

    举例:有t_person表如下: 一.mysql行拼接: 拼接某一行: 无分隔符:select   CONCAT(id,idcard,`status`,content)   from  t_perso ...

  10. Bundler和Minifier Visual Studio扩展

    原文地址:https://marketplace.visualstudio.com/items?itemName=MadsKristensen.BundlerMinifier 特征 将CSS,Java ...