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 ...
随机推荐
- OpenReadWithHttps
///<summary>///采用https协议访问网络///</summary>///<param name="URL">url地址</ ...
- 常用组件介绍 ---- Layout_weight
下面这些也可以算是组件 文本区 TextView 文本框 EditText layout 容器 view 千万不要把Layout_weight 与 Layout_width相混淆**** ...
- 强制重启Linux系统的几种方法
实际生产环境中某些情况下 Linux 服务器系统在出现致命错误需要远程进行重启,通过常规的 reboot.init 6 等方法无法正常重启(例如重启时卡在驱动程序里等情况),这时就需要通过下面介绍的几 ...
- jquery中的clone()方法
jquery中不能直接把选择到的元素如$('div')添加到其他地方,而需要使用$('div')[0]等 clone()方法直接复制HTML代码,所以可以直接用来添加元素.
- 开启Java远程调试
在JDK启动时,加入 -Xrunjdwp:transport=dt_socket,address=9900,server=y,suspend=n -Dcom.sun.management.jmxrem ...
- CentOS 配置网络
1.编辑ifcfg-eth0 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改NOBOOT=yes 3.重启服务 service network re ...
- [Linux] 网络
如何在网络中标识一台计算机 IP 多个程序如何不冲突 通信端口 不同的计算机如何通信 协议 IP A类:0+7位网络号+24位主机号,可用网络2^7-2个,每个网络可容纳2^24-2个主机 B类:10 ...
- C++钩子程序浅析
在网上搜索“键盘记录C++”实现可以找到很多相关文章,我也是照着上面的介绍去研究去试着做的,从懂到不懂.那么为什么有那么多材料我还要去写这样一篇 文章,我想这个是我个人需要关心的问题,我不是那种ctr ...
- <转> Struct 和 Union区别 以及 对内存对齐方式的说明
转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...
- 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash
[BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...