回答編集履歴

2

加筆

2019/10/22 04:07

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -52,6 +52,26 @@
52
52
 
53
53
 
54
54
 
55
+ // 重複があったらtrueを返す
56
+
57
+ // そのさん
58
+
59
+ bool is_dup_3(const std::string& str) {
60
+
61
+ // コピーを作ってソートする
62
+
63
+ std::string copy = str;
64
+
65
+ std::sort(copy.begin(), copy.end());
66
+
67
+ // 隣接する同じ要素が見つかったらtrueを返す
68
+
69
+ return std::adjacent_find(copy.begin(),copy.end()) != copy.end();
70
+
71
+ }
72
+
73
+
74
+
55
75
  int main() {
56
76
 
57
77
  std::cout << std::boolalpha << is_dup_1("abcdefg") << std::endl;
@@ -62,6 +82,10 @@
62
82
 
63
83
  std::cout << std::boolalpha << is_dup_2("abcdefd") << std::endl;
64
84
 
85
+ std::cout << std::boolalpha << is_dup_3("abcdefg") << std::endl;
86
+
87
+ std::cout << std::boolalpha << is_dup_3("abcdefd") << std::endl;
88
+
65
89
  }
66
90
 
67
91
  ```

1

追記

2019/10/22 04:07

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -1,3 +1,67 @@
1
1
  解決後でアレなのですが、
2
2
 
3
3
  「対象文字列をソートし、隣接する文字が同じ箇所があるかを調べる」のもアリかと。
4
+
5
+
6
+
7
+ C++ならラクショーなんですけどね...
8
+
9
+ ```C++
10
+
11
+ #include <iostream>
12
+
13
+ #include <string>
14
+
15
+ #include <algorithm>
16
+
17
+ #include <set>
18
+
19
+
20
+
21
+ // 重複があったらtrueを返す
22
+
23
+ // そのいち
24
+
25
+ bool is_dup_1(const std::string& str) {
26
+
27
+ // コピーを作ってソートする
28
+
29
+ std::string copy = str;
30
+
31
+ std::sort(copy.begin(), copy.end());
32
+
33
+ // 重複を取り除くことで文字列が短くなったらtrueを返す
34
+
35
+ return std::unique(copy.begin(),copy.end()) != copy.end();
36
+
37
+ }
38
+
39
+
40
+
41
+ // 重複があったらtrueを返す
42
+
43
+ // そのに
44
+
45
+ bool is_dup_2(const std::string& str) {
46
+
47
+ // 重複を許さない集合に詰め込んで、要素数が減ったら重複アリ
48
+
49
+ return std::set<char>(str.begin(), str.end()).size() != str.size();
50
+
51
+ }
52
+
53
+
54
+
55
+ int main() {
56
+
57
+ std::cout << std::boolalpha << is_dup_1("abcdefg") << std::endl;
58
+
59
+ std::cout << std::boolalpha << is_dup_1("abcdefd") << std::endl;
60
+
61
+ std::cout << std::boolalpha << is_dup_2("abcdefg") << std::endl;
62
+
63
+ std::cout << std::boolalpha << is_dup_2("abcdefd") << std::endl;
64
+
65
+ }
66
+
67
+ ```