新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)A Red Rover 【字符串/搜索相同最长子串/KMP或者暴力】

A Red Rover

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;

string s,t;

int main()
{
    while(cin>>s)
    {
        int ans;

        int n=s.size();
        ans=n;
        for(int i=0;i<n;i++)   //枚举起点
        {
            for(int j=i;j<n;j++) //不同起点下的子串终点
            {
                t += s[j]; //子串
                int p=s.find(t), c=0,lent=(int)t.length();//子串位置,个数,长度
                while(p!=string::npo) //后面找到相同子串
                {
                    c++;
                    p=s.find(t,p+lent);//继续搜索,步长每次跨子串长度个字符
                }
                ans=min(ans,n+c-c*lent+lent);
            }
            t.clear();
        }
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自www.cnblogs.com/Roni-i/p/8989008.html
今日推荐