zoukankan      html  css  js  c++  java
  • Codeforces 23A You're Given a String...

    A. You're Given a String...
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).

    Input

    The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.

    Output

    Output one number — length of the longest substring that can be met in the string at least twice.

    Examples
    input
    abcd
    output
    0
    input
    ababa
    output
    3
    input
    zzz
    output
    2
    其实就是一个字符串的问题,水题一道,但我想跟大家说说不同的做法:
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define REP(i,n) for((i)=0;(i)<(int)(n);i++)
     4 int main(){
     5     int n,i,j,k;
     6     string s;
     7     cin>>s;
     8     n=s.length();
     9     int ans=0;
    10     REP(i,n) REP(j,n) if(i<j){//两个字符串首元素位置 
    11         for(k=0;;k++) if(i+k>=n||j+k>=n||s[i+k]!=s[j+k]) break;//跑字符串长度,越界或不匹配跳出 
    12         ans=max(ans,k);//更新最大值 
    13     }
    14     cout<<ans<<endl;
    15     return 0;
    16 }
    这是最普通的做法了,O(n^3),其实不到。
    这个方法好在第三重循环,写的很妙。
    1 for(k=0;;k++) if(i+k>=n||j+k>=n||s[i+k]!=s[j+k]) break;
    跑两个字符串的首元素的位置,而不是先跑长度,这个想法很有趣。
    之后再跑长度后就可以一位一位跑了,
    这样比一个个枚举同样长度的字符串要好很多。
    对比一下先跑长度的做法:
    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        string s;
        cin>>s;
        int res=0;
        for(int l=1;l<=s.size();l++){//跑长度 
            vector<string>v; 
            for(int i=0;i+l-1<s.size();i++)
                v.push_back(s.substr(i,l));//把当前长度的字符串扔进vector里 
            sort(v.begin(),v.end());//排序准备匹配 
            for(int i=0;i<v.size()-1;i++)//开始匹配 
                if(v[i]==v[i+1]){//匹配成功,保存长度 
                    res=l;
                    break;
                }
        }
        cout<<res<<endl;
        return 0;
    }
    是不是匹配时麻烦很多?
    当然,这里通过排序匹配,其实也可以用STL里的Set来做:
    #include<bits/stdc++.h>
    using namespace std;
    int n,m;
    string w;
    set<string>all;
    int main(){
        cin>>w;
        int n=w.size();
        for(int i=n-1;i>0;i--){//跑长度 
            all.clear();//清空set 
            for(int j=0;j+i<=n;j++) all.insert(w.substr(j,i));//将子串插入集合 
            if(all.size()!=n-i+1){//集合里元素个数与总个数不一样,说明有重复,成功! 
                cout<<i<<endl;
                return 0;
            }
        }
        cout<<0<<endl;
        return 0;
    } 
    是不是也很妙?
    总结:本题是水题,但我把水题做出3种解法出来,说明这题质量很好
    希望大家在一些优秀的题上不妨多想想,这样信息学水平会提高不少的。
    (打比赛时就被这么无聊了,毕竟比赛比谁做的对,不是谁写的更好!)
     



  • 相关阅读:
    rocketMQ
    RocketMQ 事务消息
    Serial,Parallel,CMS,G1四大GC收集器特点小结
    CMS垃圾收集器与G1收集器
    CMS垃圾回收过程
    MySQL中EXPLAIN解释命令 查看索引是否生效
    redis持久化的几种方式
    深入浅出数据库索引原理
    Java中堆内存和栈内存详解
    jvm垃圾回收机制
  • 原文地址:https://www.cnblogs.com/ybwowen/p/8733062.html
Copyright © 2011-2022 走看看