TIANKENG’s restaurant(Ⅱ)

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Java/Others)
Problem Description
After improving the marketing strategy, TIANKENG has made a fortune and he is going to step into the status of TuHao. Nevertheless, TIANKENG wants his restaurant to go international, so he decides to name his restaurant in English. For the lack of English skills, TIANKENG turns to CC, an English expert, to help him think of a property name. CC is a algorithm lover other than English, so he gives a long string S to TIANKENG. The string S only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’. TIANKENG wants his restaurant’s name to be out of ordinary, so the restaurant’s name is a string T which should satisfy the following conditions: The string T should be as short as possible, if there are more than one strings which have the same shortest length, you should choose the string which has the minimum lexicographic order. Could you help TIANKENG get the name as soon as possible?

Meanwhile, T is different from all the substrings of S. Could you help TIANKENG get the name as soon as possible?

Input
The first line input file contains an integer T(T<=50) indicating the number of case.
In each test case:
Input a string S. the length of S is not large than 1000000.
Output
For each test case:
Output the string t satisfying the condition.(T also only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’.)
Sample Input
3 ABCDEFGH AAABAACADAEAFAGAH ACAC
Sample Output
AA BB B

题意:

1、找一个字符串s使得 s不是给定母串的子串

2、且s要最短

3、s在最短情况下字典序最小

解题思路:首先要肯定的一点就是,这个串s是不会超过8位的。8位串的话用dfs搜索肯定是没问题的,关键是如何判断字符串是否是子串,这里可以用哈希来解决。不过这里的哈希确实用的比较巧妙,我没有想到,h[i][ans]表示长度为i,哈希值为ans的串是否存在。这样直接传s的哈希值即可。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;const int maxn = 1000005;
int flag,h[8][2496764];
char str[maxn],res[8];bool dfs(int top,int hash,int dep)
{if(top == dep){for(int i = 0; i < 8; i++){if(h[top][hash * 8 + i] != flag){res[top] = i + 'A';res[top + 1] = 0;return true;}}return false;}for(int i = 0; i < 8; i++){res[top] = i + 'A';if(dfs(top+1,hash * 8 + i, dep)) return true;}return false;
}int main()
{int t;scanf("%d",&t);flag = 0;while(t--){flag++;scanf("%s",str);for(int i = 0; str[i]; i++){int ans = 0;for(int j = 0; j < 7 && str[i+j]; j++){ans = ans * 8 + str[i + j] - 'A';  h[j][ans] = flag;}}for(int i = 0; i < 8; i++)if(dfs(0,0,i)) break;puts(res);}return 0;
}

hdu 4886(hash + dfs)相关推荐

  1. hdu 5692 Snacks(dfs序+线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...

  2. hdu 5424(dfs搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5424 官方题解: 如果图是联通的,可以发现如果存在哈密顿路径,一定有一条哈密顿路径的一端是度数最小的点 ...

  3. hdu 4714 树+DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 本来想直接求树的直径,再得出答案,后来发现是错的. 思路:任选一个点进行DFS,对于一棵以点u为 ...

  4. ssl1127-方程的解数【HASH,dfs】

    前言 我只是凑数的. 正题 输入输出(建议无视) Input 第1行包含一个整数n.第2行包含一个整数M.第3行到第n+2行,每行包含两个整数,分别表示ki和pi.两个整数之间用一个空格隔开.第3行的 ...

  5. HDU 5305 Friends dfs

    Friends 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5305 Description There are n people and m pa ...

  6. hdu 1241Oil Deposits(dfs模板)

    题目链接-- http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,'*'表示空地,'@'表示油井.问在这个矩阵中有多少组油井区? ...

  7. HDU 1240 Asteroids!(DFS简单搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1240 Asteroids! Time Limit: 2000/1000 MS (Java/Others ...

  8. D - Age of Moyu HDU - 6386 -BFS+DFS分层最短路

    D - Age of Moyu HDU - 6386 题意:双向图,m条边,每条边有一个编号id,而花费就是根据这个id的变化来确定的求1-n的最短路 思路:全值为0,1的图直接bfs按层遍历图找到n ...

  9. Hdu 2522 hash

    题目链接 题意:输入整数n (1<= abs(n) <= 10^5) , 输出 1/n. 这题不是自己做出来的...看了网上的思路.这种题目都能想到用hash..反正我是没往那里想,看到循 ...

最新文章

  1. Java项目:精品酒店管理系统(java+SSM+mysql+maven+tomcat)
  2. python中tornado的第一个例子
  3. SAP PP ECM的生效日期如何影响工单?
  4. oracle linux内存推荐分配,浅析Oracle 的体系架构及内存分配机制
  5. 如何配置webpack的loader
  6. 回调函数与PHP实例
  7. 业务场景下数据采集机制和策略
  8. Android--List与ArrayList区别(转)
  9. 路由器的分组转发算法
  10. 使用mysql innodb 使用5.7的json类型遇到的坑和解决办法
  11. 霍夫斯特拉大学计算机科学专业排名,Hofstra University霍夫斯特拉大学介绍及排名信息...
  12. 教你用3DMAX打造个性鲜明卡通角色
  13. 解决启动eureka报错Unable to start web ... nested exception is org.springframework.boot.web.server.WebS
  14. mysql int_mysql中int(10)代表什么意思?
  15. 使用腾讯云 SCF 云函数压缩 COS 对象存储文件
  16. 红米4a android9.0,小米-红米4A/5A-通刷-LOS-Beta4.0-安卓9.0.0-来去电归属-农历等-本地化增强适配...
  17. C++编程规范 头文件格式 和 函数注释格式
  18. 大创项目:少儿编程直播平台
  19. matlab求隐函数二阶导,求隐函数二阶导数.ppt
  20. idea中好用的git shelve changes和stash changes

热门文章

  1. 神策数据通过中国信通院 SDK 安全评测
  2. PPT 下载 | 纷享销客刘晨:客户生命周期服务设计
  3. tinycore 的基本搭建,开机时间只需要1-3秒
  4. springboot添加swagger2组件
  5. Android下添加新的自定义键值和按键处理流程【转】
  6. Fragment:support.v4.content.Loader.deliverResult
  7. 不仅仅是虚拟化--Windows 7 SP1 Windows 2008R2 SP1解析
  8. gan semi conductor
  9. volunteer is great
  10. 听课的时候保证基本信息的获取