树状数组,主要是抓住要求连续1的个数。这样,初始时,相同的加1,不同的加0.

查询时,用二分搜索右边界。就是比较当前mid-l+1的值与他们之间1的个数(这可以通过树状数组求区间和得出),记录右边界即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
#define lowbit(x) ((x)&(-(x))) using namespace std;
const int N= 1000015; char str1[N],str2[N];
int ptree[N]; int sum(int *a,int x){
if(x==0) return 0;
int s=0;
for(;x;x-=lowbit(x))s+=a[x];
return s;
} void update(int *a,int x,int w,int &n){
for(;x<=n;x+=lowbit(x))a[x]+=w;
} int ans[100005]; int binSearch(int l,int r){
int p=l-1,tmp;
int spre=sum(ptree,p);
int ans=l;
while(l<=r){
int mid=(l+r)/2;
tmp=sum(ptree,mid);
if(tmp-spre<mid-p)
r=mid-1;
else{
ans=mid;
l=mid+1;
}
}
return ans-p;
} int main(){
int T,Q,kase=0;
int ch,a,ii; char c;
scanf("%d",&T);
while(T--){
cin>>str1+1;
cin>>str2+1;
int len=min(strlen(str1+1),strlen(str2+1));
memset(ptree,0,sizeof(int)*(len+5));
for(int i=1;i<=len;i++){
if(str1[i]==str2[i])
update(ptree,i,1,len);
}
scanf("%d",&Q);
printf("Case %d:\n",++kase);
for(int i=1;i<=Q;i++){
cin>>ch;
if(ch==2){
cin>>a;
if(str1[a+1]!=str2[a+1]){
printf("%d\n",0);
}
else{
printf("%d\n",binSearch(a+1,len));
}
}
else{
cin>>a>>ii>>c;
// cout<<a<<ii<<c<<endl;
if(ii+1>len) continue;
if(a==1){
if(str1[ii+1]==str2[ii+1]){
str1[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
continue;
else
update(ptree,ii+1,-1,len);
}
else{
str1[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
update(ptree,ii+1,1,len);
}
}
else{
if(str1[ii+1]==str2[ii+1]){
str2[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
continue;
else
update(ptree,ii+1,-1,len);
}
else{
str2[ii+1]=c;
if(str1[ii+1]==str2[ii+1])
update(ptree,ii+1,1,len);
}
}
}
}
}
return 0;
}

  

HDU 4339 Contest 4的更多相关文章

  1. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  2. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  3. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  4. hdu 4339 Query(两种思路求解)

    Query Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  5. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  7. HDU 5045 Contest

    pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...

  8. HDU - 4339: Query(bitset暴力找下一个为1的)

    题意:给定A,B长度相同的字符串,Q次操作,修改操作位单个字符修改,查询操作为询问从某点开始有多少连续相同的字符. 思路:我们把不相同的设为1,相同的设为0,那么询问就是找下一个为1的为位置,可以用线 ...

  9. HDU 4335 Contest 4

    利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...

随机推荐

  1. HDU 2817 EASY题

    #include <iostream> #include <cstdio> using namespace std; const __int64 MOD=200907; __i ...

  2. 【MongoDB】The basic operation of Index in MongoDB

    In the past four blogs, we attached importance to the index, including description and comparison wi ...

  3. intel dpdk在ubuntu12.04中測试testpmd、helloworld程序

    一.測试环境 操作系统:ubuntu12.04   x86_64 dpdk版本号:1.6.0r2 虚拟机:vmware 10 网卡: Intel Corporation 82545EM Gigabit ...

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览 SP2013中的OAuth

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第10章节--SP2013中OAuth概览  SP2013中的OAuth         SP apps使用OAuth授权 ...

  5. hdoj--1272--小希的迷宫(并查集)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. SQL 数据库性能优化

    http://blog.csdn.net/yzllz001/article/details/54848513 1.  减少数据访问(减少磁盘访问) 2.  返回更少数据(减少网络传输或磁盘访问) 3. ...

  7. Ubuntu14.04下沙盒数据导入到 Neo4j 数据库(图文详解)

    不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793914   (表示感谢) 前期博客 Neo4j沙盒实验申请过程 ...

  8. 爬虫概念与编程学习之如何爬取视频网站页面(用HttpClient)(二)

    先看,前一期博客,理清好思路. 爬虫概念与编程学习之如何爬取网页源代码(一) 不多说,直接上代码. 编写代码 运行 <!DOCTYPE html><html><head& ...

  9. ubuntu16.04 安装 docker

    1,切换到root 2,更新系统 # apt-get update 3,安装 https和ca证书 # apt-get install apt-transport-https ca-certifica ...

  10. visio中如何取消跨线和去掉页边距

    比较来说,写论文visio和inkscape都不可缺少. 比如visio跨线的问题,已经遇到过两次忘记了.这次截个图作为记录.其实就是在“设计”一栏里,把连接线里面的跨线显示的对勾去掉即可. *** ...