前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2493 (map)

POJ 2493 (map)

作者头像
dejavu1zz
发布2020-10-23 15:24:15
1510
发布2020-10-23 15:24:15
举报

题意描述

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

思路

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

AC代码

代码语言:javascript
复制
#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("%d\n",ans);
    }
    cout<<endl;
}
int main(){
    //IOS;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档