ACM刷题之Codeforces ————String Game
来源:互联网 发布:鬼武者 知乎 编辑:程序博客网 时间:2024/06/14 06:44
在比赛的时候这题没做出来。
因为感觉数据量太大,直接暴力的话会超时。
赛后看了下大神们的代码,发现是二分。
自己研究了下,中间错了好多次。
下面总结下这里的二分。
while(l<=r)
{
if(mids(MID(l,r)))
{
l=MID(l,r)+1;
}else{
r=MID(l,r)-1;
}
}
这里是二分的关键代码,注意每次都要 l+1 和 r-1 否则会出现死循环。
注意,最后输出答案的时候,l要减1,因为最后一次出while循环l会额外加1.
下面是ac代码
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>#include<map>#include<set>#include<queue>#include<string>#include<iostream>using namespace std;#define MID(x,y) ((x+y)>>1)#define CLR(arr,val) memset(arr,val,sizeof(arr))#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;const int N=2e5+7;int l,r,n,len2,r2;char b[200002];char a[200002];bool c[200002];int d[200002];bool mids(int mi){CLR(c,false);for(int i=1;i<=mi;i++){c[d[i]]=true;}for(int i=1,j=1;i<=r2;i++){if((!c[i])&&(a[i]==b[j])){++j;if(j>len2){return true;}}}return false;}int main(){//freopen("f:/input.txt", "r", stdin);int len1;while(scanf("%s%s",a+1,b+1)!=EOF){CLR(d,0);r=strlen(a+1);for(int i=1;i<=r;i++)scanf("%d",&d[i]);len2=strlen(b+1);r2=r;l=0;while(l<=r){if(mids(MID(l,r))){l=MID(l,r)+1;}else{r=MID(l,r)-1;}}printf("%d\n",l-1);} }
0 0
- ACM刷题之Codeforces ————String Game
- ACM刷题之codeforces————Cinema
- ACM-模拟之Candy Sharing Game——hdu1034
- ACM-巴什博弈之Brave Game——hdu1846
- ACM-博弈之Play a game——hdu1564
- ACM-对称博弈之Coin Game——hdu3951
- ACM刷题之Codeforces————Little Artem and Presents
- ACM刷题之Codeforces———— Little Artem and Grasshopper
- ACM刷题之Codeforces ————The Meeting Place Cannot Be Changed
- ACM刷题之codeforces————The Little Match Girl
- ACM刷题之codeforces————Ya Rajaie and Books
- ACM刷题之codeforces————Simple Robot
- Codeforces Round #402 (Div. 2) D String Game —— 二分法
- ACM-KMP之Count the string——hdu3336
- ACM-巴什博弈之kiki's game——hdu2147
- 杭电acm—1306 String Matching
- codeforces——119A——Epic Game
- codeforces——327A——Flipping Game
- 使用@include-media写媒介查询
- 浅谈项目组wiki维护实现方案
- 内存缓存GuavaCache源码解析
- gulp运用
- Android基础系列之Activity
- ACM刷题之Codeforces ————String Game
- Boost库的timer类讲解
- 剑指offer面试题
- 220. Contains Duplicate III
- 神经网络weight参数怎么初始化
- JAVASE--03--继承
- 数据库中表和视图的区别和联系
- 彻底弄懂JS的事件冒泡和事件捕获
- Unityeditor 事件(2)