社区讨论
TLE求助(悬关)
P1032[NOIP 2002 提高组] 字串变换(疑似错题)参与者 5已保存回复 8
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 8 条
- 当前快照
- 1 份
- 快照标识符
- @m1q7szy2
- 此快照首次捕获于
- 2024/10/01 17:07 去年
- 此快照最后确认于
- 2025/11/04 18:23 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
string s,e,x,y,ru[11];
struct node{
int val;
string ch;
bool operator<(node x)const{
return val>x.val;
}
bool operator>(node x)const{
return val<x.val;
}
};
int ans=INT_MAX,cnt;
map< string,vector<string> >rules;
map< string,int >vis;
priority_queue<node>q;
void bfs(string s){
vis[s]=true;
q.push(node{0,s});
while(!q.empty()){
node now=q.top();q.pop();
if(now.ch==e)ans=min(ans,now.val);
for(int i=1;i<=cnt;i++){
if(now.ch.length()<ru[i].length())continue;
for(int j=0;j<=now.ch.length()-ru[i].length();j++){
// cout<<j<<" "<<ru[i].length()+j<<" "<<now.ch.substr(j,ru[i].length())<<" "<<ru[i]<<endl;
if(now.ch.substr(j,ru[i].length())==ru[i]){
string temp=now.ch;
for(int k=0;k<rules[ru[i]].size();k++){
temp.replace(j,ru[i].length(),rules[ru[i]][k]);
if(vis[temp]==1||now.val+1>=10)continue;
q.push(node{now.val+1,temp});
vis[temp]=1;
// cout<<now.ch<<"--->"<<temp<<endl;
}
}
}
}
}
return;
}
int main(){
cin>>s>>e;
while(cin>>x>>y){
ru[++cnt]=x;
rules[x].push_back(y);
}
bfs(s);
if(ans>=10)cout<<"NO ANSWER!"<<endl;
else cout<<ans<<endl;
return 0;
}
TLE on #4 #5
回复
共 8 条回复,欢迎继续交流。
正在加载回复...