题目链接: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)的更多相关文章

  1. HDU 6103 Kirinriki(尺取法)

    http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...

  2. HDU 6103 Kirinriki (思维 双指针)

    Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. 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= ...

  4. 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 ...

  5. hdu 6103 Kirinriki (枚举对称中心+双指针)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑(i=0 ...

  6. 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− ...

  7. HDU 6103

    题意: 求最长的两个不相交的子序列,dis <= m : 分析: 当时二分了答案,暴力匹配,TLE了,然后考虑了,O(n^2)预处理出所有区间 dis,然后答案是所有dis中>=m的最长长 ...

  8. 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 ...

  9. 2017杭电多校第六场1008 Kirinriki

    传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

随机推荐

  1. ExtJs4学习(一):正确认识ExtJs4

    认识ExtJs 1.Javat能用ExtJs吗? 它是展现层的技术,与JS,HTML,CSS有关.至于server端是.Net,还是PHP等无关. 2.ExtJs适合什么样的项目? 依照官方的说法,E ...

  2. 36:字符串排序SortString

    题目描述:编写一个程序,将输入字符串中的字符按如下规则排序. 规则1:英文字母从A到Z排列,不区分大小写. 如,输入:Type 输出:epTy 规则2:同一个英文字母的大小写同时存在时,按照输入顺序排 ...

  3. SpringMVC请求流程与原理分析

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  4. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  5. Django之通过tag推荐文章

    #路由 views.py def post_detail(request,year,month,day,post): ''' 文章详情 + 评论详情 :param request: :param ye ...

  6. MVC中的 @helper

    ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...

  7. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. 查看并修改Linux主机名命令hostname

    查看主机名 hostname可以查看主机名 export也可以查看 修改主机名 echo new-hostname > /proc/sys/kernel/hostname (系统启动时,从此文件 ...

  9. Windows+VS+SVN实现版本控制

    Subversion已经是一个热门话题,下面介绍一下Windows下Subversion和TortoiseSVN构建SVN版本控制 问题. 首先看一些基础知识: Subversion是架设一个SVN ...

  10. hdu4612 无向图中随意加入一条边后使桥的数量最少 / 无向图缩点+求树的直径

    题意如上,含有重边(重边的话,俩个点就能够构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选随意起点U,进行bfs,到达最远的一个点v ...