题目链接:Kirinriki

题目描述:

找两个不重叠的字符串A,B。 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n-1-i} \right |\)。求最长的字符串长度。

思路:

官方题解,双指针维护。简单题。枚举对称中心。

在这里我给出我常用的双指针的写法。


  1. int a[N];
  2. int l=0,r=0,val=0;
  3. while(r没有越界) //如果满足条件
  4. {
  5. if(val+a[r]<=key) // 加上 a[r] 是否满足条件?
  6. {
  7. val+=a[r];
  8. r++;
  9. 更新最大值//满足条件的区间为 [l,r)
  10. }
  11. else //右移
  12. {
  13. val-=a[l];
  14. l++
  15. }
  16. }

下面是枚举对称轴的写法:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <set>
  6. #include <map>
  7. #include <list>
  8. #include <queue>
  9. #include <deque>
  10. #include <stack>
  11. #include <string>
  12. #include <vector>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. using namespace std;
  18. typedef long long int LL;
  19. const int INF = 2e9 + 1e8;
  20. const int MOD = 1e9 + 7;
  21. const double eps = 0.0000000001;
  22. void fre()
  23. {
  24. freopen("test.in", "r", stdin);
  25. freopen("test.out", "w", stdout);
  26. }
  27. #define MSET(a, b) memset(a, b, sizeof(a))
  28. const int maxn = 1e5 + 100;
  29. char str[maxn];
  30. int m;
  31. int len, ans;
  32. void nyist(int x,int y)
  33. {
  34. int dis=0,l=0,r=0;
  35. while(y+r<len&&x-r>=0)
  36. {
  37. if(dis+abs(str[x-r]-str[y+r])<=m)
  38. {
  39. dis+=abs(str[x-r]-str[y+r]);
  40. r++;
  41. ans=max(ans,r-l);
  42. }
  43. else
  44. {
  45. dis-=abs(str[x-l]-str[y+l]);
  46. l++;
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. int ncase;
  53. scanf("%d", &ncase);
  54. while (ncase--)
  55. {
  56. scanf("%d", &m);
  57. ans = 0;
  58. scanf("%s", str);
  59. len = strlen(str);
  60. for (int i = 0; i < len; i++)
  61. {
  62. nyist(i-1,i+1);
  63. nyist(i,i+1);
  64. }
  65. printf("%d\n", ans);
  66. }
  67. return 0;
  68. }
  69. /**************************************************/
  70. /** Copyright Notice **/
  71. /** writer: wurong **/
  72. /** school: nyist **/
  73. /** blog : http://www.cnblogs.com/coded-ream/ **/
  74. /**************************************************/

还有就是和枚举对称轴相反的写法;

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <set>
  6. #include <map>
  7. #include <list>
  8. #include <queue>
  9. #include <deque>
  10. #include <stack>
  11. #include <string>
  12. #include <vector>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. using namespace std;
  18. typedef long long int LL;
  19. const int INF = 2e9 + 1e8;
  20. const int MOD = 1e9 + 7;
  21. const double eps = 0.0000000001;
  22. void fre()
  23. {
  24. freopen("test.in", "r", stdin);
  25. freopen("test.out", "w", stdout);
  26. }
  27. #define MSET(a, b) memset(a, b, sizeof(a))
  28. const int maxn = 1e5 + 100;
  29. char str[maxn];
  30. int m;
  31. int len, ans;
  32. void nyist(int x,int y)
  33. {
  34. int dis=0,l=0,r=0;
  35. while(x+r<y-r)
  36. {
  37. if(dis+abs(str[x+r]-str[y-r])<=m)
  38. {
  39. dis+=abs(str[x+r]-str[y-r]);
  40. r++;
  41. ans=max(ans,r-l);
  42. }
  43. else
  44. {
  45. dis-=abs(str[x+l]-str[y-l]);
  46. l++;
  47. }
  48. }
  49. }
  50. int main()
  51. {
  52. int ncase;
  53. scanf("%d", &ncase);
  54. while (ncase--)
  55. {
  56. scanf("%d", &m);
  57. ans = 0;
  58. scanf("%s", str);
  59. len = strlen(str);
  60. len--;
  61. for(int i=1;i<=len;i++) nyist(0,i);
  62. for(int i=0;i<len;i++) nyist(i,len);
  63. printf("%d\n", ans);
  64. }
  65. return 0;
  66. }
  67. /**************************************************/
  68. /** Copyright Notice **/
  69. /** writer: wurong **/
  70. /** school: nyist **/
  71. /** blog : http://www.cnblogs.com/coded-ream/ **/
  72. /**************************************************/

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. C​P​U​_​C​S​t​a​t​e​_​P​S​t​a​t​e and then ACPI on Wiki

    http://wenku.baidu.com/link?url=eHbdT4EjdJx3dsQETGUIL8q1K3_EyuzGLWT0G103AEca0vs0gHR_v_3c0oaUL2gbkrr8 ...

  2. A charge WIFI point base on airbase-ng+dhcp+lamp+wiwiz

    Make wifi as a hot point Make a script echo $0 $1 case $1 in "start") sleep 1 ifconfig wla ...

  3. ASP.NET动态网站制作(3)--css(2)

    前言:css分为四次课讲完,第一节课内容见ASP.NET动态网站制作(2)--css(1),接下来的内容会涉及到定位.浮动.盒子模型(第二次课).css的具体应用(第三次课).css3(第四次课).今 ...

  4. 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法

    东北大亨: 说明:如果想在eclipse的properties文件打开的方式出现source和properties视图就需要添加JBossTools插件 下面介绍如果添加插件: 1.打开官网 http ...

  5. JVM相关小结

    对JVM中分层模型.垃圾回收期.垃圾回收算法趁着周末小结一下.有不对的地方,还请指正和讨论~ 1.JVM内存模型 2.JVM垃圾回收期  3.JVM垃圾回收算法 ------------------- ...

  6. 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】

    启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...

  7. Python:list、dict、string

    <<List>>列表 [python] view plaincopy 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 samp ...

  8. .NET Winform 将引用的dll文件集成到exe中(转)

    Winform程序经常需要引用一些第三方dll文件,这些dll在发布后与exe文件保存在同一目录下,虽然将dll文件集成到exe中会增大文件尺寸,但程序目录会相对整洁. 下面介绍一种比较简单的集成方法 ...

  9. 九度OJ 1168:字符串的查找删除 (查找)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...

  10. 解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题

    最新解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题,由系统下载吧率先分享: 有些用户在使用Windows7系统过程中,碰到到win7打印机共享出现“无法保存打印机设置 ...