题意:问是否有一个字符串包含其他字符串

思路:有的话,那一定是最长的那一个。最长的有多个的话,那这几个一定要相同;否则,不存在。跑一下AC自动机,把最长的看成目标串,别的看成模式串,看目标串中出现几次模式串。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
using namespace std;struct Trie
{int next[100010][26],fail[100010],end[100010];int root,L;int newnode(){for(int i = 0;i < 26;i++)next[L][i] = -1;end[L++] = 0;return L-1;}void init(){L = 0;root = newnode();}void insert(string &buf){int len = buf.size();int now = root;for(int i = 0;i < len;i++){if(next[now][buf[i]-'a'] == -1)next[now][buf[i]-'a'] = newnode();now = next[now][buf[i]-'a'];}end[now]++;}void build(){queue<int>Q;fail[root] = root;for(int i = 0;i < 26;i++)if(next[root][i] == -1)next[root][i] = root;else{fail[next[root][i]] = root;Q.push(next[root][i]);}while( !Q.empty() ){int now = Q.front();Q.pop();for(int i = 0;i < 26;i++)if(next[now][i] == -1)next[now][i] = next[fail[now]][i];else{fail[next[now][i]]=next[fail[now]][i];Q.push(next[now][i]);}}}int query(string &buf){int len = buf.size();int now = root;int res = 0;for(int i = 0;i < len;i++){now = next[now][buf[i]-'a'];int temp = now;while( temp != root ){res += end[temp];end[temp] = 0;temp = fail[temp];}}return res;}
};
char buf[100010];
Trie ac;
vector<string> v;
vector<int> ind;
int da;
int main()
{int T,n;string s;scanf("%d",&T);while( T-- ){scanf("%d",&n);ind.clear();v.clear();da = -1;getchar();for(int i = 0; i < n; i++){gets(buf);int len = strlen(buf);if(len > da){da = len;ind.clear();ind.push_back(i);}else if(len == da)ind.push_back(i);v.push_back(buf);}int flag = 1;if(v.size() > 1){for(int i = 0; i < ind.size(); i++){if(i > 0 && v[ind[i]] != v[ind[i-1]]){flag = 0;break;}}}if(!flag)printf("No\n");else{int ans = ind[0];ac.init();for(int i = 0;i < n;i++){if(i != ans)ac.insert(v[i]);}ac.build();int num = ac.query(v[ans]);if(num == n-1)cout<<v[ans]<<endl;elseprintf("No\n");}}return 0;
}

HDU6208 The Dominator of Strings【字符串】相关推荐

  1. hdu6208 The Dominator of Strings(AC自动机)

    题意: 给定n个串,问其中是否存在一个串,满足其他串都是它的子串. 如果存在这样的串,输出这个串,否则输出No 数据范围:所有串长度不超过1e5 解法: 答案串一定是最长的串, 如果只有一个最长的串, ...

  2. The Dominator of Strings HDU - 6208

    2018TYUT暑期ACM模拟赛(8) The Dominator of Strings HDU - 6208 题意:给你n个字符串找到一个字符串,可以包含给的所有串,如果存在就输出该字符串,不存在就 ...

  3. HDU 6208 The Dominator of Strings AC自动机

    题目链接:HDU 6208 The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535 ...

  4. [LeetCode] Isomorphic Strings - 字符串操作:数组计数字符个数问题

    题目概述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  5. Golang标准库-strings 字符串操作

    字符串处理 字符串在开发中经常用到,包括用户输入,数据库读取数据,我们经常需要对字符串进行分割,连接,转换的等操作 字符串操作 前缀和后缀 HasPrefix判断字符串是否以prefix开头 函数签名 ...

  6. COW(暴力枚举) + 玄神的字符串(思维题) + Duplicate Strings(字符串思维题)

    '只要坚持就会成功--2022.1.22' 1.COW,这道题的大概意思就是求出一个字符串中一共有多少个COW,不要求连续. 思路: 暴力枚举,枚举每一个包含O,看这个O和前后的C,W结合能组成多少个 ...

  7. Duplicate Strings 字符串 取模 牛客练习赛95

    链接:https://ac.nowcoder.com/acm/contest/11185/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52 ...

  8. C++ Strings(字符串)

    C++ Strings 构造函数(Constructors) 操作符(Operators) 添加文本(append) 赋值(assign) at begin c_str 容量(capacity) 比较 ...

  9. 43. Multiply Strings 字符串相乘

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

最新文章

  1. 阿里云服务器ecs绑定域名,端口的问题,不用80端口
  2. Java之JDK、JRE、JVM
  3. c语言file_C语言 技能提升 系列文章(七)格式化输入/输出
  4. 开源 java CMS - FreeCMS2.8 栏目页静态化参数
  5. 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
  6. MySQL / 为什么需要主键?主键为什么最好是单调递增的?
  7. php数据库创建文件失败怎么回事,安装zblogPHP提示“创建c_option.php失败”解决方法...
  8. 【问题解决】无法创建新的堆栈防护页面
  9. 数据存储方式_视频监控系统的数据存储方式的概念及应用
  10. 小米跨界成立餐饮公司?其实就是新园区食堂...
  11. Kubernetes 弃用 Docker刷爆了网络,我们公司也慌了!
  12. Delphi 源码格式书写规范
  13. Arduino 用Proteus仿真基础知识和实例
  14. c语言读写txt坐标文件数据,用c语言读写sgy格式的地震数据文件.pdf
  15. 基于CM6800的ATX电源维修
  16. Newton-Raphson算法
  17. python k折交叉验证,python中sklearnk折交叉验证
  18. 第一行代码git上传天气项目遇到问题
  19. WSO2 ——(7)ESB功能:协议转换
  20. 简易GIt-----SFile 10秒上手

热门文章

  1. Android技术分享| 【自习室】自定义View代替通知动画(完)
  2. 水肥一体化如何实现灌溉自动化
  3. Android入门项目(六)Android的wifi开发,androidwifi开发教程
  4. 华为C8650使用经验几例
  5. python获取excel特定区域_python获取excel指定区域数据库-女性时尚流行美容健康娱乐mv-ida网...
  6. 为什么快手不能左右滑了_快手不能上下滑是什么原因 | 手游网游页游攻略大全...
  7. 2023年通信工程施工与管理
  8. 服务器在国外未备案网站,选择国外服务器网站未备案也能正常打开,那网站备案到底是为了什么呢?...
  9. Spring事务注解Transactional失效
  10. Java连接MySQL mysql-connector-java-bin.jar驱动包的下载与安装