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

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

代码:

 #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
#define in freopen("F:\\rootial\\data\\data.txt", "r", stdin);
#define bug(x) printf("Line %d: >>>>>>\n", (x));
#define pb push_back
#define mp make_pair using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<pair<int, int > > VII; const ULL seed = ; const int maxn = + ;
char s[][maxn], ta[maxn];
ULL hh[][maxn], tt[maxn], HH[maxn];
void pre()
{
tt[] = ;
for(int i = ; i < maxn; i++)
tt[i] = tt[i-]*seed;
}
int n, m;
VII ans;
bool dfs(int x, int pos, int dir, int len)
{
if(len == )
return true;
int len1 = n-pos+;
int mlen = min(len, len1);
int ok = ;
if(hh[x<<|dir][pos]-hh[x<<|dir][pos+mlen]*tt[mlen] == HH[m-len+]-HH[m-len+mlen+]*tt[mlen])
{
ok = ;
ans.pb(mp(x*n+(dir ? n+-pos : pos), dir));
len -= mlen;
if(len == )
return true;
for(int k = ; k < ; k++)
for(int j = ; j < ; j++)
{
if(k == x && (j^) == dir)
continue;
if(dfs(k, , j, len))
return true;
}
}
if(ok)
ans.pop_back();
return false;
}
int main()
{ pre();
int T;
for(int t(scanf("%d", &T)); t <= T; t++)
{
scanf("%d%d", &n, &m);
for(int i = ; i < ; i++)
scanf("%s", s[i]+);
scanf("%s", ta+);
for(int i = ; i < ; i++)
hh[i][n+] = ;
HH[m+] = ;
for(int i = ; i < ; i++)
{
for(int j = n; j >= ; j--)
{
hh[i<<][j] = hh[i<<][j+]*seed+(s[i][j]-'a');
hh[i<<|][j] = hh[i<<|][j+]*seed+(s[i][n+-j]-'a');
}
}
for(int i = m; i >= ; i--)
{
HH[i] = HH[i+]*seed+(ta[i]-'a');
}
int ok = ;
for(int k = ; k < ; k++)
{
for(int pos = ; pos <= n; pos++)
{
for(int j = ; j < ; j++)
{
ok = ;
ans.clear();
if(dfs(k, pos, j, m))
{
ok = ;
break;
}
if(ok)
break;
}
if(ok)
break;
}
if(ok)
{
break;
}
}
int ll = ;
if(ok)
{
for(int i = ; i < ans.size(); i++)
{
PII x = ans[i];
int st = x.first;
int dir = x.second;
int k = (st-)/n+; if(dir == )
{
for(int j = st; j <= k*n && ll < m; j++, ll++)
{
if(ll)
putchar(' ');
printf("%d", j);
}
}
else
{
for(int j = st; j > (k-)*n && ll < m; j--, ll++)
{
if(ll)
putchar(' ');
printf("%d", j);
}
}
}
cout<<endl;
}
else
{
puts("No solution!");
}
}
return ;
}

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. rest的config

    <个人积累,转载请注明出处> 新手写rest wcf经常会报配置文件异常.我为了避免这种问题,将自己配好的config放这里,用的时候将ABC改成自己的,粘贴就行了. ABC是什么我就不赘 ...

  2. ASP多行多列又一个方法

    <table border=1 width="200"> <% col=4 '列数 i=1 Do While i<=100 If i Mod col=1 T ...

  3. Linux下chkconfig命令详解即添加服务以及两种方式启动关闭系统服务

    The command chkconfig is no longer available in Ubuntu.The equivalent command to chkconfig is update ...

  4. ie编程半天的学习总结

    自己好久没有来这个博客了,自己陆续去几个地方写博客,一个c++博客园,一个csdn. 感觉都一般吧,看不到什么好的博客,可能自己没有看到吧.以后就在这个博客记录一点技术笔记吧!自己比较懒,只要做为记录 ...

  5. 经历:Java中字符串中按照多个字符拆分或替换:split()和replaceAll()

    一.replaceAll() 今天,遇到了这样的一个字符串[如下代码]: String s="@0|新港@0|天津@0|东莞@0|南沙@0|营口@0|钦州@0|上海@0|汕头@0|连云港@0 ...

  6. 字符串 前篇 ---- sizeof()操作符和strlen()库函数

    本文不是研究sizeof(), strlen() 的深奥定义和原理,我们不会在理论上太过钻牛角尖.希望读这篇文章的你,也不要太过抠概念(不要拘泥于语法).我们只做 实用意义 的介绍和讨论. 在介绍字符 ...

  7. nodejs js模块加载

    本文地址:http://www.cnblogs.com/jasonxuli/p/4381747.html nodejs的非核心模块(core module)加载主要使用的就是module.js. 项目 ...

  8. Java 与 Python 的对比

    最近在学习Python, 现在写一个Python程序和Java程序进行对一下比,以此展示各自不同的特点.这个程序的功能是计算([n, m) )之间的闰年.     Python程序如下: def fu ...

  9. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  10. Unix/Linux之命令备忘录

    ps:是显示瞬间进程的状态,并不动态连续 kill:用于杀死进程或者给进程发送信号 // 在Linux下查看所有java进程命令 ps -ef | grep java: //  停止所有java进程命 ...