原本就想到dp,可是是我的思路是在串的各个位置都遍历一次set,看dp[i-st[k]]是否为1且前length(st[k])是st[k]。这样200000*200*10会超时。更好的办法是在i位取前len<=10个看dp[]和set中是否存在。只要200000*55*log200。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #include <bitset> #define mkp make_pair using namespace std; const double EPS=1e-8; typedef long long lon; const lon SZ=200030,INF=0x7FFFFFFF; set<string> st; string str; int arr[SZ];void init() {for(;;){string tmp;cin>>tmp;if(tmp==".")break;st.insert(tmp);}string tmp;for(;cin>>tmp;){str+=tmp;} }void work() {int res=0;for(int i=0;i<str.size();++i){for(int j=0;j<=9;++j){if(i-j-1<0||arr[i-j-1]){string tmp=str.substr(max(0,i-j),min(i+1,j+1));//if(i==6)cout<<j<<" "<<tmp<<" "<<(st.find(tmp)!=st.end())<<endl;if(st.find(tmp)!=st.end()){arr[i]=1;//cout<<i<<" "<<"here"<<endl;break;}}}if(arr[i]==1){res=i+1;}}cout<<res<<endl; }int main() {std::ios::sync_with_stdio(0);//freopen("d:\\1.txt","r",stdin); lon casenum;//cin>>casenum;//for(lon time=1;time<=casenum;++time) {init();work();}return 0; }