HDU 5510---Bazinga(指针模拟)
2018-06-17 23:43:04来源:未知 阅读 ()
题目链接
http://acm.hdu.edu.cn/search.php?action=listproblem
Don't tilt your head. I'm serious.
For n given strings S1,S2,?,Sn, labelled from 1 to n, you should find the largest i (1≤i≤n) such that there exists an integer j (1≤j<i) and Sj is not a substring of Si.
A substring of a string Si is another string that occurs in Si. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".
For each test case, the first line is the positive integer n (1≤n≤500) and in the following n lines list are the strings S1,S2,?,Sn.
All strings are given in lower-case letters and strings are no longer than 2000 letters.
#include <iostream> #include <algorithm> #include <stdio.h> #include <cstring> #include <queue> using namespace std; typedef long long LL; char s[600][2005]; int nex[2005]; void makeNext(char p[]) { int q,k; int m=strlen(p); nex[0]=0; for(q=1,k=0; q<m; ++q) { while(k>0&&p[q]!=p[k]) k=nex[k-1]; if(p[q]==p[k]) k++; nex[q]=k; } } int calc(int x,int y) { makeNext(s[x]); int l=strlen(s[x]); int len=strlen(s[y]); int q,k; for(q=0,k=0; q<len; q++) { while(k>0&&s[y][q]!=s[x][k]) k=nex[k-1]; if(s[y][q]==s[x][k]) k++; if(k>=l) return 1; } return 0; } int main() { int T,Case=1; cin>>T; while(T--) { int n; scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%s",s[i]); int l = 1, r = 2, ans = -1; while(r <= n) { while(l < r) { if(calc(l,r)) l++; else { ans=r; break; } } r++; } printf("Case #%d: %d\n",Case++,ans); } return 0; }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ this指针 2020-06-03
- 纯虚函数与基类指针数组的运用 代码参考 2020-04-30
- 成员指针与mem_fn 2020-04-23
- 第三章 类与对象进阶 2020-04-04
- C语言数组指针的理解方式 2020-03-31
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash