蒟蒻,dp求调

P1245 电话号码

orz
by Struct_Sec @ 2023-08-24 21:15:32


有一个点是你2层循环都用的i
by Struct_Sec @ 2023-08-24 21:26:16


破案了......getline的锅,改成cin就过了 另外还有一个地方要注意,你的res要赋初值,不然容易挂(当然这里没挂 ```cpp #include <stdio.h> #include <iostream> #include <string> #include <map> #include <cstring> using namespace std; bool dp[105][105]; int ans_[105][105]; map<string,bool> word; map<string , string > getwor; string password; int cti(char c) { if('a' <= c && c <= 'c') return 1; if(c <= 'f') return 2; if(c <= 'i') return 3; if(c <= 'l') return 4; if(c <= 'n') return 5; if(c <= 'q') return 6; if(c <= 't') return 7; if(c <= 'w') return 8; if(c <= 'z') return 9; return -1; } char itcn(int num) { return (char)(num + 48); } inline string getstr(string x,int l,int r) { string res; for(int i = l;i<=r;++i) { res += x[i]; } return res; } bool flag = false; void print(int l,int r) { if(ans_[l][r] == -1) { if(flag) cout<<" "; flag = true; cout<<getwor[getstr(password,l,r)]; } else { print(l,ans_[l][r]); print(ans_[l][r]+1,r); } return ; } int main() { memset(dp,0,sizeof(dp)); memset(ans_,0,sizeof(ans_)); int n; scanf("%d",&n); cin>>password; int length = password.length(); for(int i=1;i<=n;++i) { string worder,old_; cin>>worder; int len = worder.length(); old_ = worder; for(int j=0;j<len;++j) { worder[j] = itcn(cti(worder[j])); } word[worder] = true; getwor[worder] = old_; } for(int len = 1;len <= length;++len) { for(int i=0,j=i+len - 1;j < length;++i,++j) { if(word[getstr(password,i,j)] == true) { dp[i][j] = true; ans_[i][j] = -1; continue; } for(int mid = i;mid < j;++mid) { if(dp[i][mid] && dp[mid+1][j]) { dp[i][j] = true; ans_[i][j] = mid; break; } } } } if(!dp[0][length-1]) printf("No Solutions!"); else print(0,length-1); // printf("%d",dp[0][length-2]); return 0; } ```
by Struct_Sec @ 2023-08-24 21:42:32


@[Struct_Sec](/user/528908) orz 谢谢巨佬!!!
by yangyiding @ 2023-08-25 10:29:27


@[Struct_Sec](/user/528908) 但是,你是怎么发现getline有问题的?(我还没有碰到过STL里除这种问题的)
by yangyiding @ 2023-08-25 10:36:57


|