B. Hamming Distance Sum
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Examples
Input
01
00111
Output
3
Input
0011
0110
Output
2
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

题意:给你两个01串s,t ; 在t中取连续的长度为|s|的部分 求和输出

题解:对于s串中的每一个位置i的数都与t串中[i,lent-(lens-i)]计算一次,记录t串中的1的前缀乱搞。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<set>
#define ll __int64
#define mod 100000000
using namespace std;
char a[];
char b[];
int sum1[];
int sum0[];
int main()
{
scanf("%s",a+);
scanf("%s",b+);
int len1=strlen(b+);
int len2=strlen(a+);
sum0[]=;
sum1[]=;
for(int i=;i<=len1;i++)
{
sum1[i]=sum1[i-];
sum0[i]=sum0[i-];
if(b[i]=='')
sum1[i]++;
else
sum0[i]++;
}
ll ans=;
for(int i=;i<=len2;i++){
if(a[i]=='')
ans=ans+sum1[len1-(len2-i)]-sum1[i-];
else
ans=ans+sum0[len1-(len2-i)]-sum0[i-];
}
printf("%I64d\n",ans);
return ;
}
C. Chain Reaction
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note

For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.

For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.

题意:给你n个灯塔的位置与高度 现在 在最右边增加一个灯塔  输出最少能覆盖的灯塔的个数

题解:dp[i]  表示以第i个灯塔为(最右边一个没有被覆盖的灯塔)的左边的灯塔覆盖个数

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<set>
#define ll __int64
#define mod 100000000
using namespace std;
int n;
int mp[];
int dp[];
int sum[];
int a,b;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d %d",&a,&b);
mp[a]=b;
}
if(mp[]==){
sum[]=;
dp[]=;
}
else
{
sum[]=;
dp[]=;
}
int ans=1e9;;
for(int i=;i<=;i++){
sum[i]=sum[i-];
if(mp[i]!=)
sum[i]++;
}
for(int i=;i<=;i++)
{
if(mp[i]==)
dp[i]=dp[i-];
else
{
int now=max(,i-mp[i]);
if(now==)
dp[i]=sum[i-];
else
dp[i]=dp[now-]+(sum[i-]-sum[now-]);
}
ans=min(ans,dp[i]+sum[]-sum[i]);
}
printf("%d\n",ans);
return ;
}
D. Zuma
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题意:消除回文 问最少几次消除全部

题解:简单区间dp

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<bitset>
#include<set>
#define ll __int64
#define mod 100000000
using namespace std;
int n;
int a[];
int dp[][];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
dp[i][j]=1e9;
for(int i=;i<=n;i++)
dp[i][i]=;
for(int i=n;i>=;i--)
{
for(int j=i+;j<=n;j++)
{
for(int k=i;k<=j;k++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+][j]);
if(a[i]==a[j]){
if(i+==j)
dp[i][j]=;
else
dp[i][j]=min(dp[i][j],dp[i+][j-]);
}
}
}
printf("%d\n",dp[][n]);
return ;
}

Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp的更多相关文章

  1. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  2. Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】

    A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...

  3. Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp

    C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...

  4. Codeforces Round #336 (Div. 2) D. Zuma(区间DP)

    题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...

  5. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  6. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  7. Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp

    D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...

  8. Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp

    题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...

  9. Codeforces Round #235 (Div. 2) D. Roman and Numbers(如压力dp)

    Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standard i ...

随机推荐

  1. mac安装pkg 一直“正在验证” 卡着

    今天换了新mac, 但是之前wireshark(抓包工具) 不能用了 ,要安装Xquartz. 下载之后一直卡着, 网上找了半天没有解决方法. 最后我重启一下就好了... 重启一下. 2. 15款ma ...

  2. Python基础入门(迭代器和生成器)

    1 Python迭代器 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退. 迭代器有两个基本的方法:iter() 和 ...

  3. Python最简编码规范

    前言 本文是阅读<Python Coding Rule>之后总结的最为精华及简单的编码规范,根据每个人不同喜好有些地方会有不同的选择,我只是做了对自己来说最简单易行的选择,仅供大家参考. ...

  4. Amazon移除差评适用范围 - Amazon request for the feedback removal

    Greetings from Amazon Seller Support, Please accept my sincere apologies for the inconvenience cause ...

  5. 2.hive里的增删改查

    1.hive的增删改查 查询数据库 hive> show databases; OK default Time taken: 0.254 seconds, Fetched: 1 row(s) h ...

  6. USACO 2.4.4 Bessie Come Home 回家(最短路)

    Description 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只速度最快 ...

  7. Spark Transformations介绍

    背景 本文介绍是基于Spark 1.3源码 如何创建RDD? RDD可以从普通数组创建出来,也可以从文件系统或者HDFS中的文件创建出来. 举例:从普通数组创建RDD,里面包含了1到9这9个数字,它们 ...

  8. PAT---福尔摩斯约会时间

    主要为字符串的处理,注意读懂题目意思. 设置输出域宽和填充字符的函数分别为setw(int n),setfill(char c);两个函数的头文件为#include<iomanip>; # ...

  9. PHP Mailer 发送邮件

    <?php /* 下载网址 https://github.com/PHPMailer/PHPMailer 打开下载的压缩包文件目录 将 PHPMailer-master 下的 src 文件夹复制 ...

  10. 爬虫学习之-python插入mysql报错

    异常:'latin-1' codec can't encode characters in position 62-66: ordinal not in range(256) 用Python通过pym ...