éååãå¦ã¼ãï¼
åé¡æ¦è¦
åã®æåå ããã®é ã«ä¸ããããã
ååºã®æååã«å¯¾ãã¦ããã®æ·»åãåºåããã
å¶ç´
解説
0-indexed ã§èãã¾ããã¤ã¾ããæååã ã¨ãã¾ãï¼åºåããã¨ãã«ã¯ 1 ã足ãã¾ãï¼ã
ã¾ãã¯è¨ç®éã®ãã¨ãèããã«è§£ãã¦ã¿ã¾ããããä¸çªèªç¶ãªè§£æ³ã¯æ¬¡ã®ããã«ãå ã«å¯¾ãã¦ã ããã以åã«ç»å ´ãã¦ããããå¤å®ããæ¹æ³ã§ãããã
for (int i = 0; i < N; i++) { bool already = false; for (int j = 0; j < i; j++) { if (S[j] == S[i]) already = true; } if (!already) { // ååºã ã£ãã index ãåºåãã cout << i+1 << endl; } }
ãã®ã³ã¼ãã§ãµã³ãã«ã¯éãã¾ããããããæåºãã㨠TLE ã¨ãªãã¯ãã§ããããã¯ããã®ã³ã¼ãã®è¨ç®éã ã§ããããã§ãããªããè¨ç®éã«ã¤ãã¦ç¥ããªãæ¹ã¯ã次ã®è¨äºãèªãã§ã¿ã¦ãã ããã
ããã§ã¯ããã®ã³ã¼ããé«éåãã¾ããããé«éåã§ããä½å°ãããã¨ãããã
ã ããã§ã«ç»å ´ãã¦ãããã©ãããå¤å®ããã
ã¨ããé¨åã§ã¯ãªãã§ãããããããã§ãéååã®ç»å ´ã§ããC++ ã® set
åã¯æ¬¡ã®ã¯ã¨ãªãããªãããã¼ã¿æ§é ã§ãï¼å¤æ°åã se
ã¨ãã¾ãï¼ã
ã¯ã¨ãª | 詳細 | æ¸ãæ¹ | è¨ç®é |
---|---|---|---|
æ¿å ¥ | è¦ç´ ããã¼ã¿æ§é ã«æ¿å ¥ããï¼ãã§ã«ããã°ä½ãããªãï¼ | se.insert(v) |
|
åé¤ | è¦ç´ ããã¼ã¿æ§é ããåé¤ããï¼ãã§ã«ãªããã°ä½ãããªãï¼ | se.erase(v) |
|
æ¤ç´¢ | è¦ç´ ããã¼ã¿æ§é ã«å«ã¾ãããã©ãããå¤å®ãã | if (se.count(v)) |
ãªããããã§ã®è¨ç®éã¯ããã¼ã¿æ§é ã®ãµã¤ãºã ã¨ããå ´åã®ãã®ã§ãã
ãã®ãã¼ã¿æ§é ã®ãããã¨ããã¯ããã¼ã¿æ§é å
ã«è¦ç´ ãå«ã¾ãããã©ããã ã®è¨ç®éã§å¤å®ã§ãããã¨ã§ãããã® set
åãç¨ãã¦ãä¸è¨ã®ã³ã¼ãã¯æ¬¡ã®ããã«æ¸ãç´ãã¾ãã
for (int i = 0; i < N; i++) { if (!se.count(S[i])) { // ååºã ã£ãã index ãåºåãã cout << i+1 << endl; } // S[i] ããã¼ã¿æ§é ã«æ¿å ¥ãã se.insert(S[i]); }
æ¿å ¥ãæ¤ç´¢ã«è¦ããè¨ç®é㯠ã§ãã®ã§ãä¸è¨ã®è§£æ³å ¨ä½ã®è¨ç®é㯠ã¨ãªãã¾ããããã§ããã°ãååéã«åãã¾ãã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; set<string> se; cin >> N; for (int i = 0; i < N; i++) { cin >> S; if (!se.count(S)) { // ååºã ã£ãã index ãåºåãã cout << i+1 << endl; } se.insert(S); } }