传统弱校HFUT的蒟蒻,真相只有一个

字符串专题:map POJ 1002

第一次用到是在‘校内赛总结’扫地那道题里面,大同小异

map<string,int>str 可以专用做做字符串的匹配之类的处理

string donser;

str [donser]++ 自动存donser到map并且值加一,如果发现重复元素不新建直接加一,

map第一个参数是key,默认字典序升序排列key

map<string,int>::iterator ii;         map的迭代器
for(ii=outputer.begin();ii!=outputer.end();++ii)  迭代器遍历
{
  if(ii->second>1)                     map第一个参数 ii->first或者 (*ii).first ;第二个参数 ii->second或者 (*ii).second
  {
    donser=ii->first;
    cout<<donser<<" "<<ii->second<<endl;
  }
}

POJ 1002 http://poj.org/problem?id=1002

对电话号码做化数字处理,然后匹配

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
string astr;
void deal()
{
    int len=astr.size();
    for(int i=0;i<len;i++)
    {
        if(astr[i]=='-')
        {
            astr.erase(i,1);
            i--;
            continue;
        }
        if(astr[i]=='A'||astr[i]=='B'||astr[i]=='C')
        {
            astr.replace(i,1,"2");
        }
        if(astr[i]=='D'||astr[i]=='E'||astr[i]=='F')
        {
            astr.replace(i,1,"3");
        }
        if(astr[i]=='G'||astr[i]=='H'||astr[i]=='I')
        {
            astr.replace(i,1,"4");
        }
        if(astr[i]=='J'||astr[i]=='K'||astr[i]=='L')
        {
            astr.replace(i,1,"5");
        }
        if(astr[i]=='M'||astr[i]=='N'||astr[i]=='O')
        {
            astr.replace(i,1,"6");
        }
        if(astr[i]=='P'||astr[i]=='R'||astr[i]=='S')
        {
            astr.replace(i,1,"7");
        }
        if(astr[i]=='T'||astr[i]=='U'||astr[i]=='V')
        {
            astr.replace(i,1,"8");
        }
        if(astr[i]=='W'||astr[i]=='X'||astr[i]=='Y')
        {
            astr.replace(i,1,"9");
        }
    }
    return;
}
int main()
{
    int T,lable=0;
    map<string,int>outputer;
    scanf("%d",&T);
    int i=0,j=T;
    while(j--)
    {
        cin>>astr;
        deal();
        outputer[astr]++;
    }
    map<string,int>::iterator ii;
    for(ii=outputer.begin();ii!=outputer.end();++ii)
    {
        if(ii->second>1)
        {
            astr=ii->first;
            astr.insert(3,"-");
            cout<<astr<<" "<<ii->second<<endl;
            lable=1;
        }
    }
    if(!lable) cout<<"No duplicates.";
    return 0;
}

 

posted @ 2016-05-08 16:07  未名亚柳  阅读(203)  评论(0编辑  收藏  举报