题意:给定4个长度为N的字符串( N <= 100000),然后构成一个“中国结”,给定目标串,问能否从某个节点出发走一遍得到目标串,其中不能连续通过3个中心节点,也就是从字符串一个端点转移到其他端点后必须沿着这条字符串走。

分析:对4个字符串正反两面和目标串建立相应hash函数,然后暴力枚举每一个位置出发就可以了,可以有正反两个方向的走法。中间注意一下细节差不多就可以了。

代码:

  1. #include <bits/stdc++.h>
  2. #pragma comment(linker, "/STACK:102400000,102400000")
  3. #define in freopen("F:\\rootial\\data\\data.txt", "r", stdin);
  4. #define bug(x) printf("Line %d: >>>>>>\n", (x));
  5. #define pb push_back
  6. #define mp make_pair
  7.  
  8. using namespace std;
  9. typedef long long LL;
  10. typedef unsigned long long ULL;
  11. typedef pair<int, int> PII;
  12. typedef vector<pair<int, int > > VII;
  13.  
  14. const ULL seed = ;
  15.  
  16. const int maxn = + ;
  17. char s[][maxn], ta[maxn];
  18. ULL hh[][maxn], tt[maxn], HH[maxn];
  19. void pre()
  20. {
  21. tt[] = ;
  22. for(int i = ; i < maxn; i++)
  23. tt[i] = tt[i-]*seed;
  24. }
  25. int n, m;
  26. VII ans;
  27. bool dfs(int x, int pos, int dir, int len)
  28. {
  29. if(len == )
  30. return true;
  31. int len1 = n-pos+;
  32. int mlen = min(len, len1);
  33. int ok = ;
  34. if(hh[x<<|dir][pos]-hh[x<<|dir][pos+mlen]*tt[mlen] == HH[m-len+]-HH[m-len+mlen+]*tt[mlen])
  35. {
  36. ok = ;
  37. ans.pb(mp(x*n+(dir ? n+-pos : pos), dir));
  38. len -= mlen;
  39. if(len == )
  40. return true;
  41. for(int k = ; k < ; k++)
  42. for(int j = ; j < ; j++)
  43. {
  44. if(k == x && (j^) == dir)
  45. continue;
  46. if(dfs(k, , j, len))
  47. return true;
  48. }
  49. }
  50. if(ok)
  51. ans.pop_back();
  52. return false;
  53. }
  54. int main()
  55. {
  56.  
  57. pre();
  58. int T;
  59. for(int t(scanf("%d", &T)); t <= T; t++)
  60. {
  61. scanf("%d%d", &n, &m);
  62. for(int i = ; i < ; i++)
  63. scanf("%s", s[i]+);
  64. scanf("%s", ta+);
  65. for(int i = ; i < ; i++)
  66. hh[i][n+] = ;
  67. HH[m+] = ;
  68. for(int i = ; i < ; i++)
  69. {
  70. for(int j = n; j >= ; j--)
  71. {
  72. hh[i<<][j] = hh[i<<][j+]*seed+(s[i][j]-'a');
  73. hh[i<<|][j] = hh[i<<|][j+]*seed+(s[i][n+-j]-'a');
  74. }
  75. }
  76. for(int i = m; i >= ; i--)
  77. {
  78. HH[i] = HH[i+]*seed+(ta[i]-'a');
  79. }
  80. int ok = ;
  81. for(int k = ; k < ; k++)
  82. {
  83. for(int pos = ; pos <= n; pos++)
  84. {
  85. for(int j = ; j < ; j++)
  86. {
  87. ok = ;
  88. ans.clear();
  89. if(dfs(k, pos, j, m))
  90. {
  91. ok = ;
  92. break;
  93. }
  94. if(ok)
  95. break;
  96. }
  97. if(ok)
  98. break;
  99. }
  100. if(ok)
  101. {
  102. break;
  103. }
  104. }
  105. int ll = ;
  106. if(ok)
  107. {
  108. for(int i = ; i < ans.size(); i++)
  109. {
  110. PII x = ans[i];
  111. int st = x.first;
  112. int dir = x.second;
  113. int k = (st-)/n+;
  114.  
  115. if(dir == )
  116. {
  117. for(int j = st; j <= k*n && ll < m; j++, ll++)
  118. {
  119. if(ll)
  120. putchar(' ');
  121. printf("%d", j);
  122. }
  123. }
  124. else
  125. {
  126. for(int j = st; j > (k-)*n && ll < m; j--, ll++)
  127. {
  128. if(ll)
  129. putchar(' ');
  130. printf("%d", j);
  131. }
  132. }
  133. }
  134. cout<<endl;
  135. }
  136. else
  137. {
  138. puts("No solution!");
  139. }
  140. }
  141. return ;
  142. }

ZOJ 3817 Chinese Knot的更多相关文章

  1. 数学+高精度 ZOJ 2313 Chinese Girls' Amusement

    题目传送门 /* 杭电一题(ACM_steps 2.2.4)的升级版,使用到高精度: 这次不是简单的猜出来的了,求的是GCD (n, k) == 1 最大的k(1, n/2): 1. 若n是奇数,则k ...

  2. zoj 2313 Chinese Girls' Amusement 解题报告

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313 题目意思:有 N 个人(编号依次为1~N)围成一个圆圈,要求求 ...

  3. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  4. 使用MySQL数据库将汉字转换成拼音的一个C语言小程序

    环境: mysql:mysql-5.1.65 centos:centos 6.5 编译命令: gcc -o chinesetopinyin chinesetopinyin.c -L/usr/lib/m ...

  5. A - Chinese Girls' Amusement ZOJ - 2313(大数)

    You must have heard that the Chinese culture is quite different from that of Europe or Russia. So so ...

  6. ZOJ 3817Chinese Knot(The 2014 ACM-ICPC Asia Mudanjiang Regional First Round)

    思路: 将4个串每个串都反向这样得到新的四个串一共8个串,对于母串每个位置检测这个串能不能放进去,hs或者后缀数组都可以.然后dp[i][j]  (0<i<len  0<=j< ...

  7. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  8. CTRL-Space always toggles Chinese IME (Windows 7、10)

    一.window占用了ctrl+空格的快捷键,影响开发工具的只能提示的使用. 二.解决方式: Go to Start > Type in regedit and start it (打开运行输入 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. VMware下LINUX的虚拟机增加磁盘空间

    先关闭虚拟机电源,做如下设置:“ 虚拟机”--“虚拟机设置”--“磁盘”--“扩展” 可以随意添加你需要增到到的磁盘大小(如15Gb,表示磁盘总量,包含原来的磁盘容量); 再重启电源进入系统做如下步骤 ...

  2. c#局域网文件搬移

    /// kongxiang--2013.7.23 /// using System;using System.Collections.Generic;using System.Linq;using S ...

  3. MS SQLserver数据库安装

    SQL2008的安装 1,双击打开setup安装文件 2,点击“全新安装或向现有安装添加功能” 3,安装程序支持规则,安装完之后,点击确定 4,输入产品的密钥,点击下一步 5,弹出“安装程序支持文件” ...

  4. 转:ASP.NET MVC 3 and App_Code folder

    问题: In ASP.NET Webform, App_Code is standardfolder to putting code and using it at run-time.But I th ...

  5. 06_init()和destroy()方法

    [工程截图] [HelloWorld.java] package com.HigginCui; public class HelloWorld { public HelloWorld(){ Syste ...

  6. cannot convert from '_TCHAR *' to 'char *'

    Reference: Why can't convert TCHAR* to char* Need to Use Multi-Byte Character Set in project's setti ...

  7. AngularJS(2)-Scope作用域和控制器

    $scope: 根作用域 所有的应用都有一个 $rootScope,它可以作用在 ng-app 指令包含的所有 HTML 元素中. $rootScope 可作用于整个应用中.是各个 controlle ...

  8. div重叠不变形

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. JDK和Jython安装

    下载JAVA SE,下载地址请到oracle官方网站下载. JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  10. Google Code Jam 2014 Qualification 题解

    拿下 ABD, 顺利晋级, 预赛的时候C没有仔细想,推荐C题,一个非常不错的构造题目! A Magic Trick 简单的题目来取得集合的交并 1: #include <iostream> ...