USACO--3.1Contact

來源:互聯網
上載者:User

標籤:usaco

字串處理。我是直接用的map記錄的所有的子串,然後再從map中取出結果排序,然後就輸出。其實這個題都是0-1串,開始的時候我是想著能不能用hash的方法來記錄每個子串,但是後面覺得00 000 0000 這些情況不好處理,後來看了官網題解發現我們可以在這個串的最前面的加上一個1然後就可以用二進位hash的思想求解了。
PS:這道題的輸出格式蠻坑的。。。。。。

代碼如下

/*ID:15674811LANG:C++PROG:contact*/#include<iostream>#include<cstdio>#include<cstring>#include<map>#include<algorithm>using namespace std;#define INF 0x3f3f3f3f#define maxn 1000000typedef struct{    string str;    int cnt;}P;P p[maxn];bool cmp(P p1,P p2){    int len1=p1.str.size();    int len2=p2.str.size();    if(p1.cnt==p2.cnt)    {        if(len1==len2)        {            return p1.str<p2.str;        }        else        {            return len1<len2;        }    }    return p1.cnt>p2.cnt;}string str,str1;int main(){    freopen("contact.in","r",stdin);    freopen("contact.out","w",stdout);    //freopen("lkl.txt","r",stdin);    map<string,int>M;    int a,b,n;    cin>>a>>b>>n;    str="";    while(cin>>str1)    {        str+=str1;    }    int len=str.size();    for(int i=0;i<len;i++)    {        for(int j=a;j<=b&&i+j<=len;j++)        {            str1=str.substr(i,j);            M[str1]++;        }    }    map<string,int>::iterator it;    int k=1;    for(it=M.begin();it!=M.end();it++)    {        p[k].str=it->first;        p[k].cnt=it->second;        k++;    }    sort(p+1,p+k,cmp);    int cnt=1,cur=p[1].cnt,tmp=1;    cout<<p[1].cnt<<endl;    cout<<p[1].str;    for(int i=2;i<k;i++)    {         if(p[i].cnt==p[i-1].cnt)         {             if(cnt==0)                cout<<p[i].str;             else                cout<<" "<<p[i].str;             cnt++;             if(cnt==6)             {                cout<<endl;                cnt=0;             }         }         else         {             if(tmp==n)                break;             if(cnt!=0)               cout<<endl;             cout<<p[i].cnt<<endl;             cout<<p[i].str;             cnt=1;             tmp++;         }    }    if(cnt!=0)       cout<<endl;  return 0;}

USACO--3.1Contact

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.