hdu 6208 The Dominator of Strings
来源:互联网 发布:淘宝介入手机页面取消 编辑:程序博客网 时间:2024/06/06 08:39
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6208
题意:给出n个字符串,判断某一个字符串是否包含了其他所有字符串。
分析:首先这个字符串一定是最长的那个,如果有多个最长的也没关系,任意找一个就行,如果有多个最长的字符串,则他们必须相同。方法一,c++里面的find函数;方法二,kmp算法;方法三,字典树;(以后给出)方法四,AC自动机。(未学,代码以后给出)
代码:(方法一)
#include<iostream>#include<algorithm>#include<string>using namespace std;const int maxn = 1e5+5;string str[maxn];int t,n,flag;void input(){ cin>>n; int mxl=0; for(int i=0; i<n; i++){ cin>>str[i]; if(str[i].size()>mxl){ mxl=str[i].size(); flag=i; } }}int judge(string a,string b){ string::size_type position; position=a.find(b); ///find 函数 返回b 在a 中的下标位置 ///如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295, if(position!=a.npos)///找到了 return 1; return 0;}void slove(){ for(int i=0; i<n; i++){ if(!judge(str[flag],str[i])){ cout<<"No"<<endl; return ; } } cout<<str[flag]<<endl;}int main(){ cin>>t; while(t--){ input(); slove(); }}另外再附上c++find函数详解http://www.cnblogs.com/balingybj/p/4679401.html(转载)。
STL真是强大啊,刷题少费了很多功夫,得多用。
方法二:(由题意可知,字符串的最大长度或个数大约为1e5,用二维char字符数组会内存超限,必须用c++的string类!!!)
#include<iostream>#include<algorithm>#include<cstring>#include<string>using namespace std;const int maxn = 1e5+5;string str[maxn];void getNext(string s,int *next){ int i=0,j=-1,len=s.size(); next[0]=-1; while(i<len){ if(j==-1||s[i]==s[j]) next[++i]=++j; else j=next[j]; }}int kmp(string s1,string s2,int *next){ int i=0,j=0,l1=s1.size(),l2=s2.size(); while(i<l1&&j<l2){ if(j==-1||s1[i]==s2[j]) i++,j++; else j=next[j]; } if(j==l2) return 1; return 0;}int main(){ int t,n; cin>>t; while(t--){ cin>>n; int mxl=0,flag;///字符串的最大长度、下标标记 for(int i=0;i<n;i++){ cin>>str[i]; if(str[i].size()>mxl) mxl=str[i].size(),flag=i; } string pat=str[flag];///模式串 int next[maxn],flag1=0; for(int i=0;i<n;i++){ if(i==flag) continue; getNext(str[i],next); if(!kmp(pat,str[i],next)){ flag1=1; break; } } if(!flag1) cout<<pat<<endl; else cout<<"No"<<endl; }}
阅读全文
0 0
- HDU 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- hdu 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings
- HDU 6208 The Dominator of Strings [AC自动机]
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208:The Dominator of Strings(字符串匹配)
- HDU 6208 The Dominator of Strings(字符串)
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings(String查找)
- HDU 6208 The Dominator of Strings (kkkkkk....mp)
- HDU 6208 The Dominator of Strings 后缀自动机
- HDU 6208 The Dominator of Strings (AC自动机)
- hdu 6208 The Dominator of Strings 后缀自动机 LCS
- HDU 6208 The Dominator of Strings AC自动机
- HDU 6208 The Dominator of Strings(stl)
- deep learning 调试
- 记录一下spring的设置注入 构造器注入
- sklearn 数据加载,数据归一,特征选择,逻辑回归,贝叶斯,k近邻,决策树,SVM
- python中字符编码问题详解
- intellij 配置jrebel
- hdu 6208 The Dominator of Strings
- eclipse中tomcat能正常启动,但是浏览器访问不了tomcat首页 问题解决
- jquery获取select选中文本值
- 九宫格问题
- TCP数据报首部
- 使用Math.random生成随机数
- Python基础1
- 委托
- mac 安装