• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
煎蛋啊
博客园    首页    新随笔    联系   管理    订阅  订阅
字符串分类问题(牛客第二场笔试)

牛牛有N个字符串,他想将这些字符串分类,他认为两个字符串A和B属于同一类需要满足以下条件:
A中交换任意位置的两个字符,最终可以得到B,交换的次数不限。比如:abc与bca就是同一类字符串。
现在牛牛想知道这N个字符串可以分成几类。

 

#include "stdio.h"
#include <vector>
#include <deque>
#include <stack>
#include<map>
#include<utility>
using namespace std;

class Sort {
public:
  int find(vector<string> res, int n) {
        // write code here
        int count = 0;
        if(n>50)
            return 0;

        map<string,int> stringMap;
        map <string,int>::iterator iter,iterend;
        for(int i=0;i<n;i++)
        {
            sort(res[i].begin(),res[i].end());
            stringMap.insert(pair<string,int>(res[i],res[i].size()));
        }
        iterend = stringMap.end();
        for(iter=stringMap.begin();iter!=iterend;iter++)
        {
            count++;
        }
       /* for(int i=0;i<n;i++)
        {
            for(int j=0;j<res[i].size();j++)
            {
                cout<<res[i][j]<< " ";
            }
            cout<<endl;
        }
        */
        return count;
  }
};
int main() {
    int num;
    string str;
    vector<string> res;
   // while(cin>>num)
    {
        cin>>num;

        for(int i=0;i<num;i++)
        {
            cin>>str;
            res.push_back(str);
        }
    }



    Sort solution;
    int result = solution.find(res,num);
    cout<<"RESULT:"<<result<<endl;
   return 0;
}

 

posted on 2017-03-25 14:39  煎蛋啊  阅读(1699)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3