九度oj 题目1165:字符串匹配

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3972

解决:1413

题目描述:

    读入数据string[ ],然后读入一个短字符串。要求查找string[ ]中和短字符串的所有匹配,输出行号、匹配字符串。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如“aa[123]bb”,就是说aa1bb、aa2bb、aa3bb都算匹配。

输入:

输入有多组数据。
每组数据第一行输入n(1<=n<=1000),从第二行开始输入n个字符串(不含空格),接下来输入一个匹配字符串。

输出:

输出匹配到的字符串的行号和该字符串(匹配时不区分大小写)。

样例输入:
4
Aab
a2B
ab
ABB
a[a2b]b
样例输出:
1 Aab
2 a2B
4 ABB
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdio>
 
using namespace std;
 
 
int main()
{
    //freopen("f:\\test.txt", "r", stdin);
    string str, baseStr;
    int n;
    vector<string> vstr;
    vector<string> vstr2;
    while (cin >> n) {
        for (int i = 0; i < n; ++i) {
            cin >> str;
            vstr.push_back(str);
        }
        getchar();
        getline(cin, baseStr);
        string s;
        string s1, s2;
        string s3;//用于取子串
        string::size_type ipos;
        //cout << "before :"<< baseStr << endl;
        while ((ipos = baseStr.find(" ")) != string::npos)//去掉多余空格
            baseStr.replace(ipos, 1, "");
        //cout << "after:" << baseStr << endl;
         
        if ((ipos = baseStr.find('[')) != string::npos) {
            s1 += baseStr.substr(0, ipos);
            s = baseStr.substr(ipos+1, baseStr.find(']')-ipos-1);
            s2 = baseStr.substr(baseStr.find(']') + 1);
            //cout << "s1 is:" << s1 << endl;
            for (int i = 0; i < s.size(); i++) {
                s3 = s1 + s[i] + s2;
                //cout << "befor s3 is:" << s3 << endl;
                for (int j = 0; j < s3.size(); j++) {
                    s3[j] = tolower((s3)[j]);
                }
                //cout << "s3 is:" << s3 << endl;
                vstr2.push_back(s3);
            }
 
            for (int i = 0; i < vstr.size(); i++) {
                string stmp = "";
                for (int j = 0; j < vstr[i].size(); j++) {
                    stmp += tolower((vstr[i])[j]);
                }
                for (int k = 0; k < vstr2.size(); k++) {
                    if (stmp == vstr2[k]) {
                        cout << i + 1 << " " << vstr[i] << endl;
                    }
                }
            }
        }
        else {
            s = baseStr;
            for (int i = 0; i < s.size(); i++) {
                s[i] = tolower(s[i]);
            }
            //cout << "s is:" << s << endl;
            for (int i = 0; i < vstr.size(); i++) {
                string stmp = "";
                for (int j = 0; j < vstr[i].size(); j++) {
                    stmp += tolower((vstr[i])[j]);
                }
                if (stmp == s) {
                    cout << i + 1 <<" " <<vstr[i] << endl;
                }
            }
        }
        vstr2.clear();
        vstr.clear();
    }
    return 0;
}
/**************************************************************
    Problem: 1165
    User: wedkjh2
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1528 kb
****************************************************************/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值