SPOJ 665. String it out
来源:互联网 发布:营销软件网站源码 编辑:程序博客网 时间:2024/05/16 07:29
刚看到这个题时还以为是用动规求最长公共子列的方法做,其实只要一个for枚举判断循环的长度就可以了。想复杂了。。。。。。。。
#include<cstdio>#include<cstring>#include<iostream>#define MAXN 500010using namespace std;int main(){ //freopen("in.txt","r",stdin); char a[MAXN],b[MAXN]; int cas,lena,lenb; cin>>cas; getchar(); while(cas--) { cin>>a>>b; lena=strlen(a),lenb=strlen(b); if(lena>lenb) { cout<<0<<endl; continue; } else if(lena==lenb) { if(strcmp(a,b)==0) cout<<1<<endl; else cout<<0<<endl; continue; } int flag=0,i; for(i=lenb/lena; i>=0; i--) { int cur=0,num=0; for(int j=0; b[j]!='\0'; j++) { if(b[j]==a[cur]) num++; if(num==i) { cur++; num=0; } if(a[cur]=='\0') { flag=1; break; } } if(flag) break; } if(flag) cout<<i<<endl; else cout<<0<<endl; } return 0;}
下面这个用二分求解的,不是我的代码,放在这里对比学习。
#include<cstdio>#include<iostream>#include<cstring>#define MAXN 500020using namespace std;char a[MAXN],b[MAXN];int isok(int m){ int cur=0,num=0; for(int i=0; b[i]!='\0'; i++) { if(b[i]==a[cur]) num++; if(num==m) { cur++; num=0; } if(a[cur]=='\0') return 1; } return 0;}int main(){ // freopen("in.txt","r",stdin); int cas,lena,lenb; cin>>cas; getchar(); while(cas--) { cin>>a>>b; lena=strlen(a),lenb=strlen(b); if(lena>lenb) { cout<<0<<endl; continue; } else if(lena==lenb) { if(strcmp(a,b)==0) cout<<1<<endl; else cout<<0<<endl; continue; } int x=0,y=lenb/lena; while(x<y) { int m=(y-x)/2+x; if(isok(m)) { x=m+1; } else y=m; } if(isok(x)) cout<<x<<endl; else if(x>0) cout<<x-1<<endl; else cout<<0<<endl; } return 0;}
- SPOJ 665. String it out
- SPOJ:665 String it out
- Sorting It All Out
- Sorting It All Out
- Sorting It All Out
- Sorting It All Out
- poj1094Sorting It All Out
- Sorting It All Out
- log4j ckeck it out
- dom4j check it out
- Sorting It All Out
- poj1094Sorting It All Out
- Sorting It All Out
- Sorting It All Out
- poj1094Sorting It All Out
- NYOJ349_Sorting It All Out
- Out-String 帮助信息
- C#中的out string
- UITextField 用法
- myeclipse如何实现接口中的方法
- SQL_学生信息管理系统(图文详解)
- 第二章 - 有意义的命名 - 读书心得
- C#基础知识回顾--串行化与反串行化
- SPOJ 665. String it out
- 学习 ICTCLAS 字典中保存函数
- android service后台服务进程的自启动和保持不被杀了
- 高性能网站建设指南-读书笔记
- mysql
- JSP include参数的中文乱码问题
- 网络开发及程序设计书籍精选
- CXF发布WebService到Weblogic 10.3.5上遇到的问题
- [小说]魔王冢(28)二龙山