H4P2[Sorting Words]反思

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了H4P2[Sorting Words]反思相关的知识,希望对你有一定的参考价值。

Problem 2: Sorting Words

Description

Implement a program that reads from a user-specified input file, and stores the words into a vector, then sort the vector. A function object should be passed to sort(), which accepting two strings as parameters. If the length of the first string is shorter than that of the second, the function object returnstrue. Output the sorted words to a file 3_2out.txt. All words should be small letters.

Input Format

You‘re supposed to get the path of the article from the standard input device. The path may be either relative or complete. Each path is guaranteed existence.

The article file contains several lines. Those words in it may be captial letters or small letters. Captial words like LOVE and love should be regard as the same word.

Output Format

You ought to accomplish the above requirements.

Sample Input

Standard Input

article.txt

article.txt

The Maximum-Subarray Problem

Description
Suppose that you been offered the opportunity to invest in the Volatile Chemical Corporation. Like the chemicals the company produces, the stock price of the Volatile Chemical Corporation is rather volatile. You are allowed to buy one unit of stock only one time and then sell it at a later date, buying and selling after the close of trading for the day. To compensate for this restriction, you are allowed to learn what the price of the stock will be in the future. Your goal is to maximize your profit.

A brute-force solution
Try every possible pair of buy and sell dates in which the buy date precedes the sell date.

Sample Output

3_2out.txt

a
be
at
it
is
of
in
to
the
try
day
for
and
one
buy
are
you
sell
then
been
this
what
that
will
like
pair
your
date
unit
only
time
goal
which
stock
every
price
close
later
dates
after
learn
buying
rather
future
profit
invest
offered
trading
allowed
selling
company
problem
suppose
produces
possible
precedes
maximize
volatile
chemical
solution
chemicals
compensate
corporation
restriction
brute-force
description
opportunity
maximum-subarray

 

MyCode:

2016.04.01.0:24

 1 class Compare
 2 {
 3 public:
 4     bool operator() (const string &s1, const string &s2)
 5     {
 6         if (s1.length() == s2.length())
 7             return s1 < s2;
 8         else
 9             return s1.length() < s2.length() ? true : false;
10     }
11 };
12 
13 class CIsPunct
14 {
15 public:
16     bool operator() (const char c)
17     {
18         return ispunct(c) == 0 ? false : true;
19     }
20 }; 

 


 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<algorithm>
 5 #include<fstream>
 6 #include<cctype>
 7 #include"2.h"
 8 using namespace std;
 9 
10 int main()
11 {
12     vector<string> vstr;
13     string filename;
14     ifstream in(filename);
15     ofstream out("3_2out.txt");
16     cin >> filename;
17 
18     if (!in)
19     {
20         cerr << "error";
21         return 1;
22     }
23 
24     string temp;
25     while (in >> temp)
26     {
27         temp.erase(remove_if(temp.begin(), temp.end(), CIsPunct()), temp.end());//找到标点符号并删除
28         transform(temp.begin(), temp.end(), temp.begin(), ::tolower);//把每个单词变成小写
29         vstr.push_back(temp);
30     }
31 
32     sort(vstr.begin(), vstr.end(), Compare());//对所有单词排序(根据单词长度,默认<运算符)
33     auto iter = unique(vstr.begin(), vstr.end());//移除相邻的重复单词
34     vstr.erase(iter, vstr.end());//彻底删除多余的单词
35 
36     for (const auto &c : vstr)
37         out << c << endl;
38 
39     system("pause");
40     return 0;
41 }

 

列下 花费时间长的几个坑和新用法 加以记忆:

用到了:函数对象,文件的输入输出,几个泛型算法(sort,transform, remove_if, )

 

 

  • int ispunct ( int c );
  • template <class ForwardIterator> ForwardIterator unique (ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);

  • template <class InputIterator, class OutputIterator, class UnaryOperation> OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op);

template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation> OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);

 

以上是关于H4P2[Sorting Words]反思的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5649 DZY Loves Sorting(二分答案+线段树线段树合并+线段树分割)

leetcode 692. Top K Frequent Words 题解

leetcode 692. 前K个高频单词

lintcode-easy-Longest Words

2021.5.20(刷题-前K个高频单词)

力扣练习006---前K个高频单词(692)