2017-11 (not fresh grad ) find all substring with N size and only one duplicate character.

可能是2018 亚麻的OA题。

1.给字符串, 找出里面长度为N的, 并且里面字符只有一次重复的字串。
例子:s: asdfaghjkjqoiiii;N=5.  返回asdfa ghjkj hjkjq jkjqo jqoii.

 1 #include <iostream>     // std::cout
 2 #include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
 3 #include <vector>       // std::vector
 4 #include <unordered_map>
 5 #include <unordered_set>
 6 #include <numeric>
 7 #include <sys/time.h>
 8 #include <list>
 9 #include <map>
10
11 //main point : ( refer to key data structure and the trick  )
12
13 // use the two unordered set: dup and match
14 //  dup : for check if the finded substring is already in result set , that means it has been found early.
15 //  match: check if the substring has only one duplicated char.
16
17 //use list to store the results.
18
19 // iterate the string with N size window from first char to the end of the string.
20
21
22 using namespace std;
23
24
25 list<string> findNSubstring (string& s, int N){
26     //check the input
27     if ( s.empty()|| s.size() < N ) return list<string>();
28
29     int sp =0;
30     int steps = N-1;
31     cout << " the steps is " << steps << endl;
32     unordered_set<string> dup;  //make sure no duplicated substring in result set.
33     unordered_set <char> match;   // check the substring has only one duplicated char.
34     list<string> res;    //store the output.
35
36     // Check the substrings one by one with N size window.
37     while ((sp+steps) < s.size()){
38         match.clear();
39
40         // check if the substring have only one duplicated character.
41         for (int j =0; j < N ; j++){
42             match.insert(s[sp+j]);
43         }
44
45         if (match.size() == N-1){ // if yes, check if that substring has been in result set.
46             cout<< "find one " <<  s.substr( sp, N)  << " at "<< sp << endl;
47             if ( dup.find( s.substr( sp, steps)) == dup.end()){
48                 res.push_back(s.substr( sp, N));
49                 dup.insert (s.substr( sp, N));
50             }
51         }
52         sp++;
53     }
54     return res;
55 }
56
57 int main () {
58
59    // cout << " test  a substring length : " << string ("").size() << endl;
60
61     //get the start time.
62     struct timeval tv;
63     gettimeofday(&tv,NULL);
64     long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000;
65
66     //*** call the function .
67
68     string in = "iiiiiiiiiiiiiiiiiii";//asdfaghjkjqoiiii";
69
70     int q = 100 ;
71
72     auto out = findNSubstring(in, q) ;
73
74     for (auto e : out)
75         cout<< e << endl;
76     //*** end of the call
77
78     //get the time of end.
79     gettimeofday(&tv,NULL);
80     long te = tv.tv_sec * 1000 + tv.tv_usec / 1000;
81
82     //output the time of the running.
83     cout<<endl<< endl<< "running tmie is : " << te - ts << endl;
84     return 0;
85 }

原文地址:https://www.cnblogs.com/HisonSanDiego/p/8295998.html

时间: 2024-08-30 18:36:22

2017-11 (not fresh grad ) find all substring with N size and only one duplicate character.的相关文章

2017/11/07_那么明显的坑你还往里跳 Cannot set property &#39;innerHTML&#39; of null

学习react,使用webpack构建工具 在html引入生成的bundle.js时,写成了这样子: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script src="bundle.js"></scri

2017.11.15 String、StringBuffer、StringBuilder的比较

参考来自:http://blog.csdn.net/jeffleo/article/details/52194433 1.速度 一般来说,三者的速度是:StringBuilder > StringBuffer > String. 但是,在String a = "how" + "old" + "are" + "you".这种直接拼接的情况下,String速度最高.这是因为jvm的优化问题,jvm会自动识别,把&quo

2017.11.11 B201 练习题思路及解题方法

2017.11.11 B201 练习题思路及解题方法 题目类型及涵盖知识点 本次总共有6道题目,都属于MISC分类的题目,涵盖的知识点有 信息隐藏 暴力破解 音轨,摩斯电码 gif修改,base64原理 序列密码 各题的解题思路及过程 签到题:隐写诶.jpeg[知识点:信息隐藏] 本题为 隐写诶.jpeg 的图片文件,可以对该文件尝试一些基本的图片隐写解题思路,如将文件后缀名由 .jpeg 更改为 .txt 后利用记事本打开,或直接对文件点击右键后 打开方式→记事本打开,在打开的记事本窗口中获得

2017.11.25【NOIP提高组】模拟赛A组

2017.11.25[NOIP提高组]模拟赛A组 T1 3467. [NOIP2013模拟联考7]最长上升子序列(lis) T2 3468. [NOIP2013模拟联考7]OSU!(osu) T3 3472. [NOIP2013模拟联考8]匹配(match) T1 有转移方程f[i]=max{f[j]}+1,a[j]<a[i] 可以用线段树+离散化维护这个方程,因为涉及以往状态可以用主席树维护 打太丑爆空间了 Code 1 #include<cstdio> 2 #include<c

[LOJ 6249]「CodePlus 2017 11 月赛」汀博尔

Description 有 n 棵树,初始时每棵树的高度为 H_i,第 i 棵树每月都会长高 A_i.现在有个木料长度总量为 S 的订单,客户要求每块木料的长度不能小于 L,而且木料必须是整棵树(即不能为树的一部分).现在问你最少需要等多少个月才能满足订单. Input 第一行 3 个用空格隔开的非负整数 n,S,L,表示树的数量.订单总量和单块木料长度限制.第二行 n 个用空格隔开的非负整数,依次为 H1,H2,…,Hn.第三行 n 个用空格隔开的非负整数,依次为 A1,A2,…,An. Ou

51CTO学院新课发布~~带你遇见更好的自己(六)(2017.11.20-11.26)

 一周的时间匆匆即逝,又到了给你们出新课列表的时候了,小编每周都辛苦的给你们推课,也不几道你们到底看了没,想到小编之前做讲师的时候,那可是一把鼻涕一把泪的催着同学们学习. 有个段子特别能描述当时的心情:"老师这个职业吧,说的文明点就是每天带着学生在知识的海洋里畅游.然而畅游一段时间吧,你会发现:只有你一个人上岸了!!!!!!!然后你还得返回,一个一个去捞.有些吧,昨天捞上来今天又掉下去了还得捞.在你喘息的时候,你会惊恐地发现:还有往回游的".So,跟我一起游往这周的新课列表吧,锵锵锵~

51CTO学院新课发布~~带你遇见更好的自己(七)(2017.11.27-12.03)

以往新课发布的开场白,都是小编姐姐逗比的闲扯,小编姐姐准备转变一下画风,以后的新课发布开场白就谈谈每周我对于职场或者生活的一点小理解吧. 上周看到一篇文章,关于人和人的身价的差距:职场10年,为什么有人已经当上了董事总经理,而有的人还是资深销售经理?为什么有人已经当上了架构师,而有的人还是资深技术人员?为什么有人已经身价数十亿美金,而有的人还在为竞争总监头衔而周游于人情场?人和人的身价几倍甚至几十倍的差距,真的就只是智商.教育背景.能力.勤奋程度所决定的吗?当然不是.更大程度上是由个人的价值观.

51CTO学院新课发布~~带你遇见更好的自己(九)(2017.11.04-12.17)

新的一周新的失望,大家好,你们的毒鸡汤姐又华丽丽的上线了.今天想跟大家聊聊这个投资.为什么要说这个呢?因为最近小编的妈妈(一位三四线小城市的中年妇女),居然开始玩区域链了,这使得小编不寒而栗,毕竟我现在都没彻底搞清楚区域链.曾记得我是2014年的时候开始听说比特币,身边有买比特币的朋友也一直跟我分享他们的喜悦.因为今年比特币的大幅上涨,形成了一种投资热潮.随之而来的区域链.加密货币等等获得了更多投资者的关注.不少投资者开始寻找下一个比特币,于2011年推出的莱特币或许是其中一个备选.曾经莱特币在

2018-2 N substring with only one duplicate character

这是亚麻2018 年新题的第一题: // find all the N substring with only one duplicate character. #include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector #inc