欢迎光临散文网 会员登陆 & 注册

AcWing在线题库_4398. 查询字符串

2022-04-17 17:14 作者:Clayton_Zhou  | 我要投稿

// https://www.acwing.com/problem/content/description/4401/

//

#include<iostream>

#include <cstdio>

#include <string>

#include <algorithm>

#include <unordered_map>


/*

map 提供的是一种键值对容器,里面的数据都是成对出现的.

每一对中的第一个值称之为关键字(key),每个关键字只能在 map 中出现一次;

第二个称之为该关键字的对应值。

*/


using namespace std;

unordered_map<string, int> mp;

unordered_map<string, string> ans;

int n, q, cnt;

string f[10010]={

"",

"test",

"contests",

"test.",

".test"

};


string sub[10]={

"ts",

".",

"st.",

".test",

"contes.",

"st",

};

int main() {

n=4;

   /* cin >>n;

    for (int i = 1; i <= n; i ++ )

        cin >> f[i];*/

    for (int i = 1; i <= n; i ++ ) {

        unordered_map<string, bool> flag;

        int len = f[i].size();

        for (int j = 0; j < len; j ++ ) 

{

            string s;

            for (int k = j; k < len; k ++ ) 

{

                s += f[i][k];// 生成字串

if(!mp[s]) ans[s] = f[i];

                if(flag[s] == false){

mp[s] ++; //在同一个 f[i] 中,一个子串只能加 1 次。

flag[s] = true;     

}

            }

        }

    }

/*

     cin >>q;

    while (q -- ) 

{

        string s; cin >> s;

        if(mp[s] == 0) printf("0 -\n");

        else cout << mp[s] << " " << ans[s] << endl;

    }

*/

for (int i = 0; i <6; i ++ ) {

if(mp[sub[i]] == 0) printf("0 -\n");

        else cout << mp[sub[i]] << " " << ans[sub[i]] << endl;

}


    return 0;

}


AcWing在线题库_4398. 查询字符串的评论 (共 条)

分享到微博请遵守国家法律