p1470 Longest Prefix
生活随笔
收集整理的這篇文章主要介紹了
p1470 Longest Prefix
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原本就想到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; }?
轉載于:https://www.cnblogs.com/gaudar/p/9824399.html
總結
以上是生活随笔為你收集整理的p1470 Longest Prefix的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注解原理
- 下一篇: 0基础lua学习(二十)Lua单例