hdu 6103(Kirinriki)
题目链接:Kirinriki
题目描述:
找两个不重叠的字符串A,B。 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n-1-i} \right |\)。求最长的字符串长度。
思路:
官方题解,双指针维护。简单题。枚举对称中心。
在这里我给出我常用的双指针的写法。
int a[N];
int l=0,r=0,val=0;
while(r没有越界) //如果满足条件
{
if(val+a[r]<=key) // 加上 a[r] 是否满足条件?
{
val+=a[r];
r++;
更新最大值//满足条件的区间为 [l,r)
}
else //右移
{
val-=a[l];
l++
}
}
下面是枚举对称轴的写法:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long int LL;
const int INF = 2e9 + 1e8;
const int MOD = 1e9 + 7;
const double eps = 0.0000000001;
void fre()
{
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
#define MSET(a, b) memset(a, b, sizeof(a))
const int maxn = 1e5 + 100;
char str[maxn];
int m;
int len, ans;
void nyist(int x,int y)
{
int dis=0,l=0,r=0;
while(y+r<len&&x-r>=0)
{
if(dis+abs(str[x-r]-str[y+r])<=m)
{
dis+=abs(str[x-r]-str[y+r]);
r++;
ans=max(ans,r-l);
}
else
{
dis-=abs(str[x-l]-str[y+l]);
l++;
}
}
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &m);
ans = 0;
scanf("%s", str);
len = strlen(str);
for (int i = 0; i < len; i++)
{
nyist(i-1,i+1);
nyist(i,i+1);
}
printf("%d\n", ans);
}
return 0;
}
/**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://www.cnblogs.com/coded-ream/ **/
/**************************************************/
还有就是和枚举对称轴相反的写法;
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long int LL;
const int INF = 2e9 + 1e8;
const int MOD = 1e9 + 7;
const double eps = 0.0000000001;
void fre()
{
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
#define MSET(a, b) memset(a, b, sizeof(a))
const int maxn = 1e5 + 100;
char str[maxn];
int m;
int len, ans;
void nyist(int x,int y)
{
int dis=0,l=0,r=0;
while(x+r<y-r)
{
if(dis+abs(str[x+r]-str[y-r])<=m)
{
dis+=abs(str[x+r]-str[y-r]);
r++;
ans=max(ans,r-l);
}
else
{
dis-=abs(str[x+l]-str[y-l]);
l++;
}
}
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &m);
ans = 0;
scanf("%s", str);
len = strlen(str);
len--;
for(int i=1;i<=len;i++) nyist(0,i);
for(int i=0;i<len;i++) nyist(i,len);
printf("%d\n", ans);
}
return 0;
}
/**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://www.cnblogs.com/coded-ream/ **/
/**************************************************/
hdu 6103(Kirinriki)的更多相关文章
- HDU 6103 Kirinriki(尺取法)
http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...
- HDU 6103 Kirinriki (思维 双指针)
Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)
题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...
- HDU - 6103 :Kirinriki(不错的尺取法)
We define the distance of two strings A and B with same length n is dis A,B =∑ i=0 n−1 |A i −B n−1−i ...
- hdu 6103 Kirinriki (枚举对称中心+双指针)
Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑(i=0 ...
- HDU 6103 17多校6 Kirinriki(双指针维护)
Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...
- HDU 6103
题意: 求最长的两个不相交的子序列,dis <= m : 分析: 当时二分了答案,暴力匹配,TLE了,然后考虑了,O(n^2)预处理出所有区间 dis,然后答案是所有dis中>=m的最长长 ...
- hdu some problems in Multi-University Training Contest
hdu 6103 Kirinriki #include<bits/stdc++.h> using namespace std; int n,m,ans; ]; void doit(int ...
- 2017杭电多校第六场1008 Kirinriki
传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- 【Python】创建和使用类
面向对象编程是最有效的软件编写方法之一 创建Dog类 class Dog(): '''一次模拟小狗的简单测试''' def __init__(self,name,age): self.name = n ...
- shell脚本安装ntp server 服务
##############################Deploy ntp server ######################## echo "start deploy ntp ...
- Android fragment (二)
怎样使用fragment? 1.首先你要确定下你有多少个fragment要使用在一个activity里. 2.依据你的fragment的数量,创建继承自fragment的class.然后依据实际需求重 ...
- MFC——9.多线程与线程同步
Lesson9:多线程与线程同步 程序.进程和线程是操作系统的重点,在计算机编程中.多线程技术是提高程序性能的重要手段. 本文主要解说操作系统中程序.进程和线程之间的关系,并通过相互排斥对象和事件对象 ...
- hdu 2063过山车
二分匹配 #include<iostream> #include<cstring> using namespace std; int k,m,n; int rem[500+5] ...
- rtems 4.11 RTC驱动 (arm, beagle)
RTC驱动的框架在 c/src/lib/libbsp/shared/tod.c 中,大部分功能都已经实现了,入口函数是 rtc_initialize(),BSP要实现的东西非常少. beagle的实现 ...
- 30天自制操作系统(三)进入32位模式并导入C语言
1 制作真正的IPL IPL(Initial Program Loader),启动程序装载器,但是之前并没有实质性的装载任何程序,这次作者要开始装载程序了. 虽然现在开发的操作系统啥功能也没有,作者说 ...
- 深入Asyncio(十一)优雅地开始与结束
Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...
- Docker入门系列5:常见问题小结
重启容器 再次运行容器: docker start container_id 然后 docker attach container_id 就可以继续下命令了. [编辑]命名 --name [编辑]端口 ...
- bash学习记录
bash: 管理员: 提示符# 普通用户:提示符$ 环境变量 A=3(变量是指内存空间,A指的是内存空间的名称-变量标示符) PS1 \u@\h:\w\$ \u用户名 \h主机名 \w工作目录的 ...