首页 > 其他 > 详细

p1470 Longest Prefix

时间:2018-10-21 12:02:17      阅读:137      评论:0      收藏:0      [点我收藏+]

原本就想到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;
}

 

p1470 Longest Prefix

原文:https://www.cnblogs.com/gaudar/p/9824399.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!