【noip 2002】字串变换
来源:互联网 发布:java面试宝典下载 编辑:程序博客网 时间:2024/06/05 15:48
去题面的传送门
BFS
很暴力很暴力,因为数据范围实在是太小了
wa在了hash上。
以后hash的底数还是选质数吧
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int mo=2333,e=301;int cnt,hash_A,hash_B,Ans;int la[30],lb[30];string A,B;string a[10],b[10];bool vis[100000];struct lxt{ int hash,num; string s;};queue<lxt>q;int bfs(){ int lA=A.length(); int lB=B.length(); for(int i=0;i<lA;++i) hash_A=(hash_A%mo+(A[i]*e)%mo)%mo; q.push((lxt){hash_A,0,A}); vis[hash_A]=true; while(!q.empty()) { string s; s=q.front().s; int num=q.front().num; q.pop(); int lu=s.length(); for(int k=1;k<=cnt;++k) { for(int i=0;i<lu;++i) { int j; for(j=0;j<la[k];++j) if(s[i+j]!=a[k][j]) break; if(j==la[k]) { string ans; int where=i; for(int t=0;t<where;++t) ans+=s[t]; ans+=b[k]; for(int t=where+la[k];t<lu;++t) ans+=s[t]; if(ans==B) { if(num<10)return num+1; return -1; } int hash=0; for(int t=0;t<lu-la[k]+lb[k];++t) hash=((hash%mo+(ans[t])%mo)*e%mo)%mo; if(!vis[hash]) { vis[hash]=true; q.push((lxt){hash,num+1,ans}); } } } } } return -1;}int main(){ cin>>A>>B; while(cin>>a[++cnt]) { cin>>b[cnt]; la[cnt]=a[cnt].length(); lb[cnt]=b[cnt].length(); } Ans=bfs(); if(Ans==-1) printf("NO ANSWER!"); else printf("%d",Ans); return 0;}
阅读全文
1 0
- 【NOIP 2002】字串变换
- 【noip 2002】字串变换
- 【NOIp 2002】【BFS+STL】字串变换
- NOIP 2002 提高组 复赛 字串变换
- NOIP 2002 提高组 复赛 字串变换
- [NOIP提高组2002]字串变换
- NOIP 2002字串变换 解题报告(双向宽搜)
- 【2002提高】字串变换
- 字串变换
- 字串变换
- 字串变换
- 字串变换
- 搜索练习 2002t2字串变换
- NOIP 2015 字串
- noip 2015 day2T2 字串
- 【noip 2015】字串 dp
- 双向搜索 --- 字串变换
- Wikioi P1099 字串变换
- CNN实现MNIST手写数字识别
- springAOP背后的原理
- Shell脚本的符号
- liferay中JSP页面的跳转
- 简单数组练习(for+if)
- 【noip 2002】字串变换
- 二叉树面试总结 算法 java
- linux系统基本命令之软件管理
- 其实就是 VR?关于微软 Windows MR,你需要知道的都在这里
- C语言提高-36讲: 二维数组及其指针内容的实践
- 傀儡乎?黑马乎?Uber 新CEO确定 Kalanick回归难度加大
- ofo小黄车宣布进入奥地利,每半小时4元
- Linux IO概述
- C语言和C++区别——C和C++之间互相调用