STL练习 (string, map, vector, sort)

Anagrams (II)
时间限制: 1 Sec 内存限制: 128 MB

题目描述
One of the preferred kinds of entertainment of people living in final stages of XX century is filling in the crosswords. Almost every newspaper and magazine has a column dedicated to entertainment but only amateurs have enough after solving one crossword. Real professionals require more than one crossword for a week. And it is so dull - just crosswords and crosswords - while so many other riddles are waiting out there. For those are special, dedicated magazines. There are also quite a few competitions to take part in, even reaching the level of World Championships. Anyway - a lot.

You were taken on by such a professional for whom riddle solving competing is just a job. He had a brilliant idea to use a computer in work not just to play games. Somehow anagrams found themselves first in the line. You are to write a program which searches for anagrams of given words, using a given vocabulary, tediously filled with new words by yours employer.
输入
The structure of input data is given below:

<word 1>


<test word 1>


END
is an integer number N < 1000. <word 1> up to are words from the vocabulary. <test word 1> up to are the words to find anagrams for. All words are lowercase (word END means end of data - it is NOT a test word). You can assume all words are not longer than 20 characters.
输出
For each list the found anagrams in the following way:
Anagrams for:
)

should be printed on 3 chars.

In case of failing to find any anagrams your output should look like this:

Anagrams for:
No anagrams for:
样例输入 Copy
1
8
atol
lato
microphotographics
rata
rola
tara
tola
pies
tola
kola
aatr
photomicrographics
END
样例输出 Copy
Anagrams for: tola

  1. atol
  2. lato
  3. tola
    Anagrams for: kola
    No anagrams for: kola
    Anagrams for: aatr
  4. rata
  5. tara
    Anagrams for: photomicrographics
  6. microphotographics

又是一道用STL非常方便的题呐~

题意: t组测试样例, 每组给定容量为n的字典, 接下来给你若干单词, 查找字典中是否存在该单词, 并把所有的匹配的单词按要求格式打印出来 (与Anagrams Ⅰ一样, 可经过重新排列变成一样的单词视为同一单词, e.g. dog & god).

思路: 采用每输入一个单词就将它全部转化为小写并按字典序排序, 接着将原单词存入与转化后单词具有映射关系的字符串数组中, 后面询问时只需再次对所询问单词进行相同处理, 输出对应字符串数组中的所用元素即可. 代码如下:

#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define RE register int
#define INF 0x3f3f3f3f
#define EPS 1e-8
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> P;
const double Pi = acos(-1);
const int mod = 1e9 + 7;
const int N = 1e2 + 10;
const ll inf = 1e15;
int t, n;

inline string trans(string s) {
    for (auto & i : s) i = tolower(i);
    sort(s.begin(), s.end());
    return s;
}

int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cin >> t;

    while (t--) {
        map<string,vector<string>> m;
        string ori, tmp; cin >> n;

        while (n--) {
            cin >> ori; tmp = trans(ori);
            m[tmp].push_back(ori);
        }

        while (cin >> ori && ori != "END") {
            tmp = trans(ori);
            cout << "Anagrams for: " << ori << endl;

            if (m[tmp].size())
                for (RE i = 0, cnt = 1; i < m[tmp].size(); i++)
                    cout << "  " << cnt++ << ") " << m[tmp][i] << endl;
            else cout << "No anagrams for: " << ori << endl;
        }
        if (t) cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值