P1003

Description

作为一名网络警察,你的任务是监视电子邮件,看其中是否有一些敏感的关键词。不过,有些狡猾的犯罪嫌疑人会改变某些单词的字母顺序,以逃避检查。请编写一个程序,发现这种调整过顺序的关键词。程序的输入有两行,第一行是关键词列表,第二行是待检查的句子。程序的输出为在该句子中所找到的经过顺序调整的关键词。(单词全部为小写,单词之间以一个空格分隔,每一行的单词个数不限)

Input

第一行是关键词列表,第二行是待检查的句子。

Output

经过顺序调整的关键词。(单词全部为小写,单词之间以一个空格分隔,每一行的单词个数不限)

Sample Input 1 

guns mines missiles
aameric ssell snug dan iimsssle ot sit neeemis

Sample Output 1

guns missiles

思路:

读取每一行中以空格划分的单词,使用divideSen 和 getSingleWord 函数;如何 判断 关键词和乱序的待检查单词是否相等,使用 isSameLetter 函数。

代码如下

 #include<iostream>
#include<vector>
#include<sstream>
using namespace std;
string sort(string &a)//排序函数(必须以&a传参的形式,不然没办法改变)
{
    char t;
    for (int i = 0; i < a.length(); i++)
    {
        for (int j = i+1; j < a.length(); j++)
        {
            if (a[i] >= a[j])
            {
                t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
        }
    }
    return a;
}
int main()
{
    string s,c;
    getline(cin, s);//以行的形式输入字符串(即可以读入空格)
    getline(cin, c);
    vector<string>cha;
    istringstream ss(s);
    vector<string>arr;
    string t;
    while(ss>>t)//用字符流以空格分开字符串
    {
        cha.push_back(t);//存敏感词
        arr.push_back(t);//存排序后的敏感词
    }
    vector<string>sortcha;
    istringstream cc(c);
    while (cc>>t)
    {
        sortcha.push_back(t);//存排序后的待检测的词
    }
    for (int i = 0; i < arr.size(); i++)
    {
        sort(arr[i]);//排序敏感词
    }
    for (int i = 0; i < sortcha.size(); i++)
    {
        sort(sortcha[i]);//排序待检测的词
    }
    int cnt = 0;
    for (int i = 0; i < arr.size(); i++)
    {
        for (int j = 0; j < sortcha.size(); j++)
        {
            if (sortcha[j] == arr[i])//相等输出对应位置的敏感词即可
            {
                if (cnt != 0)
                    cout << ' ';
                cout << cha[i];
                cnt++;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值