Reverse and Compare


Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

You have a string A=A1A2…An consisting of lowercase English letters.

You can choose any two indices i and j such that 1≤ijn and reverse substring AiAi+1…Aj.

You can perform this operation at most once.

How many different strings can you obtain?

Constraints

  • 1≤|A|≤200,000
  • A consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

A

Output

Print the number of different strings you can obtain by reversing any substring in A at most once.


Sample Input 1

Copy
aatt

Sample Output 1

Copy
5

You can obtain aatt (don't do anything), atat (reverse A[2..3]), atta (reverse A[2..4]), ttaa (reverse A[1..4]) and taat (reverse A[1..3]).


Sample Input 2

Copy
xxxxxxxxxx

Sample Output 2

Copy
1

Whatever substring you reverse, you'll always get xxxxxxxxxx.


Sample Input 3

Copy
abracadabra

Sample Output 3

Copy
44

//很简单的一道DP,但是难想到,想到极其简单
dp[i]为前 i 个数的不同串数的话,dp[i] = dp[i-1] + sum[ str[j]!=str[i] ] (1<=j<i) (前面不等于字符 i 的所有字符的个数)
因为,只要不同,就能reverse[j,i]出一个新串,相同时,reverse[j+1,i-1]是和前相同的,算过了
此题,容易想到回文串什么的,回文串就是个坑,根本不对,跳进去就很难出来了
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
#define MX 200500 LL dp[MX];
LL num[];
char s[MX]; int main()
{
scanf("%s",s+);
int len = strlen(s+);
dp[]=;
for (int i=;i<=len;i++)
{
dp[i]=dp[i-];
for (int j=;j<;j++)
{
if (s[i]=='a'+j) continue;
dp[i] += num[j];
}
num[ s[i]-'a' ]++;
}
printf("%lld\n",dp[len]);
return ;
}

Reverse and Compare(DP)的更多相关文章

  1. AtCoder Grand Contest 019 B - Reverse and Compare【思维】

    AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ...

  2. [CSP-S模拟测试]:reverse(数位DP)

    题目描述 我们定义: $\overline{d_k...d_2d_1}=\sum \limits_{i=1}^kd_i\times {10}^{i-1}=n(d_i\in [0,9]\ and\ d_ ...

  3. codeforces#1234F. Yet Another Substring Reverse(子集dp)

    题目链接: https://codeforces.com/contest/1234/problem/F 题意: 给出一个只包含前20个小写字母的字符串,一次操作可以让一段字符颠倒顺序 最多一次这样的操 ...

  4. AtCoder Grand Contest 019 B: Reverse and Compare

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

  5. B - Reverse and Compare 小小思维题

    http://agc019.contest.atcoder.jp/tasks/agc019_b 一开始的做法是, 用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案. 然后,如果翻转afucka ...

  6. Leetcode第1题至第10题 思路分析及C++实现

    笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC.(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了, ...

  7. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  8. AtCoder Grand Contest 019

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

  9. [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String

    In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...

随机推荐

  1. Sublime Theme

    Package Control Messages======================== Theme - Spacegray----------------- Thanks for insta ...

  2. 倍福TwinCAT(贝福Beckhoff)应用教程13.1 TwinCAT控制松下伺服 NC高级

    本节主要演示了使用自定义函数实现电机的运动(梯形曲线和S曲线都有实现),这里的JOG+和JOG-,针对单个关节实现了PTP的运动(跟贝福的MoveAbsolute功能块实现效果一致),在此没有介绍运动 ...

  3. 【Excle】在方框内打勾

    在excel中,输入☑可以用控件,也可以用设置windings 2字体来设置. 如下图所示D列,字体设置成Wingdings 2字体后,输入R显示☑,输入S显示☒. 下面实现2个功能 从下拉菜单输入 ...

  4. 人工智能: 自动寻路算法实现(四、D、D*算法)

    博客转载自:https://blog.csdn.net/kongbu0622/article/details/1871520 据 Drew 所知最短路经算法现在重要的应用有计算机网络路由算法,机器人探 ...

  5. XAOP的使用示范例子

    代码地址如下:http://www.demodashi.com/demo/12976.html XAOP 一个简易的AOP(Android)应用框架.囊括了最实用的AOP应用. 特点 支持快速点击切片 ...

  6. js 页面离开前触发事件

    当前窗口载入新的dom文档前发生 window.onbeforeunload = function(event) { return confirm("确定离开此页面吗?");

  7. Fiddler4插件开发实践

    Fiddler4 是一款 巴拉巴拉..... 连接在这:http://www.telerik.com/fiddler 开发文档在这:http://docs.telerik.com/fiddler/Ex ...

  8. android自定义View&&简单布局&&回调方法

    一.内容描述 根据“慕课网”上的教程,实现一个自定义的View,且该View中使用自定义的属性,同时为该自定义的View定义点击事件的回调方法. 二.定义自定义的属性 在res/valus/ 文件夹下 ...

  9. nginx环境下启动php-fpm

    nginx环境下启动php-fpm 1.首先查看是否安装了php-fpm 这个我试了好多命令都不行比如 rpm -qa php-fpm , rpm -ql php-fpm , which php-fp ...

  10. URAL 1750 Pakhom and the Gully 计算几何+floyd

    题目链接:点击打开链接 gg.. . #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cs ...