Reverse and Compare(DP)
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≤i≤j≤n 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
aatt
Sample Output 1
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
xxxxxxxxxx
Sample Output 2
1
Whatever substring you reverse, you'll always get xxxxxxxxxx.
Sample Input 3
abracadabra
Sample Output 3
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)的更多相关文章
- AtCoder Grand Contest 019 B - Reverse and Compare【思维】
AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ...
- [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_ ...
- codeforces#1234F. Yet Another Substring Reverse(子集dp)
题目链接: https://codeforces.com/contest/1234/problem/F 题意: 给出一个只包含前20个小写字母的字符串,一次操作可以让一段字符颠倒顺序 最多一次这样的操 ...
- AtCoder Grand Contest 019 B: Reverse and Compare
题意: 给出一个字符串,你可以选择一个长度大于等于1的子串进行翻转,也可以什么都不做.只能翻转最多一次. 问所有不同的操作方式得到的字符串中有多少个是本质不同的. 分析 tourist的题妙妙啊. 首 ...
- B - Reverse and Compare 小小思维题
http://agc019.contest.atcoder.jp/tasks/agc019_b 一开始的做法是, 用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案. 然后,如果翻转afucka ...
- Leetcode第1题至第10题 思路分析及C++实现
笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC.(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了, ...
- [cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...
- AtCoder Grand Contest 019
最近比较懒,写了俩题就跑了 A - Ice Tea Store 简化背包 #include<cstdio> #include<algorithm> using namespac ...
- [Swift]LeetCode777. 在LR字符串中交换相邻字符 | Swap Adjacent in LR String
In a string composed of 'L', 'R', and 'X'characters, like "RXXLRXRXL", a move consists of ...
随机推荐
- ASP.NET MVC之单元测试分分钟的事
一.为什么要进行单元测试? 大部分开发者都有个习惯(包括本人在内),常常不喜欢去做单元测试.因为我们对自己写的程序总是盲目自信,或者存在侥幸心理每次运行通过后就直接扔给测试组的妹子们了.结果妹子一测, ...
- B11:解释器模式 Iterpreter
给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. UML: 示例代码: abstract class Expression { abstract pub ...
- JavaScript原生函数(内置函数)
1.JavaScript原生函数(内置函数) JavaScript原生函数(内置函数)有: String() Number() Boolean() Array() Object() Function( ...
- zoj 2744 - Palindromes
题目:统计一个串中的回文子串的个数(注意是子串,要连续). 分析:dp.暴力.直接用dp,二维数组内存不够用,并且dp木有暴力快( ⊙ o ⊙ )啊! 说明:(2011-09-24 03:22). # ...
- MongoDB笔记(二):MongoDB下Shell的基本操作
一.mongoDB与关系型数据库对比 对比项 mongoDB 关系型数据库(oracle.mysql) 表 集合List 二维表table 表的一行数 ...
- C#绑定事件时使用匿名函数
当使用一些临时的函数 可以预知这些函数基本不会被复用时 可以使用匿名函数简化代码 public static void startCoupons() { //绑定一些事件 userGetCoupon ...
- 一些移动端的ui框架
一些移动端的ui框架 https://jqweui.cn/resource
- unity, 显示像素图,以及iOS下像素图变模糊解决办法
在PS里画了个16x16像素的图: 在webplayer下Filter Mode选为Point,显示效果为: 在ios下显示效果为: 是由于iOS下会将图片压缩为pvr所致,想得到清晰的效果,需将Fo ...
- 在CTreeCtrl控件点击事件中获取点击的项
网上搜了一下,有两种方法: 1.使用GetSelectedItem() HTREEITEM hItem = m_treeCtrl.GetSelectedItem(); CString strText ...
- mockito模拟静态方法
这里要用到使用powerMock 注意点: 1 @RunWith(PowerMockRunner.class) 2 PowerMockito.mockStatic(StaticTest.class); ...