POJ 2493 (map)

时间:2022-07-28
本文章向大家介绍POJ 2493 (map),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

给定有n个词的字典和m个句子,每个词语除首位和末位的字母可以任意移动,求能够构成m个句子的总共不同情况有多少

思路

由于每个词语的中间位置可以任意改变,所以我们对每个词语中间的位置进行排序,并用map来记录排序后的词语出现的次数。对于m个句子,由于有空格的存在,所以我们可以使用getline来读取一行并用istringstream来对空格进行分割。

AC代码

#include<iostream>
#include<cstdio>
#include<sstream>
#include<string>
#include<map>
#include<algorithm>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int Case=1;
map<string,int> cnt;
void solve(){
    cnt.clear();
    int n;cin>>n;
    rep(i,0,n){
        string s;cin>>s;
        if(s.size()>2) sort(s.begin()+1,s.end()-1);
        cnt[s]++;
    }
    int m;cin>>m;getchar();//注意吃掉一个空格,否则会影响句子的读入
    printf("Scenario #%d:n",Case++);
    rep(i,0,m){
        string s;getline(cin,s);
        string word;
        int ans=1;
        istringstream in(s);
        while(in>>word){
            if(word.size()>2) sort(word.begin()+1,word.end()-1);
            ans*=cnt[word];
        }
        printf("%dn",ans);
    }
    cout<<endl;
}
int main(){
    //IOS;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}