zoukankan      html  css  js  c++  java
  • UVa156

    题意:

           输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排得到输入文本中的另外一个单词。在判断是否满足条件时,字母不区分大小写,但在输出时应该保留输入中的大小写,按字典序进行排列。

    分析:

           将输入的单词进行“标准化”,即将单词中的每个字母化为小写并按字典序重排单词,用一个字典来统计一个标准化的单词出现过多少次,输出的时候只输出在标准字典中出现一次的那些单词即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <map>
     4 #include <vector>
     5 #include <algorithm>
     6 using namespace std;
     7 // 将单词进行标准化
     8 string repr(const string& s){
     9     string ans = s;
    10     for(int i = 0 ; i < ans.length() ; i++)
    11         ans[i] = tolower(ans[i]);
    12     sort(ans.begin(),ans.end());
    13     return ans;
    14 }
    15 vector<string> words;
    16 map<string,int> cnt;
    17 int main(){
    18     int n = 0;
    19     string s;
    20     while(cin >> s){
    21         if(s[0] == '#') break;
    22         words.push_back(s);
    23         string r = repr(s);
    24         if(!cnt.count(r)) cnt[r] = 0;
    25         cnt[r]++;
    26     }
    27     vector<string> ans;
    28     for(int i = 0 ; i < words.size() ; i++)
    29         if(cnt[repr(words[i])] == 1) ans.push_back(words[i]);
    30     sort(ans.begin(),ans.end());
    31     for(int i = 0 ; i < ans.size() ; i++)
    32         cout << ans[i] << endl;
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5834387.html
Copyright © 2011-2022 走看看