본문 바로가기

Algorithm

(2139)
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1181번:단어정렬 답 12345678910111213141516171819202122232425#include #include #include using namespace std;//길이가 같으면 사전순으로//길이가 다르면 더 작은순으로//중복 제거bool cmp(const string &a, const string &b){ if (a.length() == b.length())//길이가 같으면 사전 순으로 return a n; for (int i = 1; i > a[i]; sort(a + 1, a + n+1,cmp); for (int i = 1; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 10926번:??1 답 12345678#include #include using namespace std;int main() { string k; cin >> k; cout
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 9316번:Hello Judge 답 12345678910#include using namespace std;int main() { int n; cin >> n; for(int i =1; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1325번:효율적인 해킹(DFS) 답 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950//가장 많이 연결되어 있는 간선을 가지고 있는 점을 찾아야 한다.#include #include #include #include using namespace std;int n, m,u,v,c[10001],d[10001];//각 정점에 연결된 간선의 개수가 몇인지 저장하는 vector a[10001];vector p(10001);//가장 많은 간전을 가지고 있는 점을 저장함void DFS(int x){ c[x] = 1; d[x]++; for (int i = 0; i > n >> m; while (m--) { cin >> u >> v; a[u]...
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1325번:효율적인 해킹(BFS) 답 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960//가장 많이 연결되어 있는 간선을 가지고 있는 점을 찾아야 한다.#include #include #include #include using namespace std;int n, m,u,v,c[10001],d[10001];//각 정점에 연결된 간선의 개수가 몇인지 저장하는 vector a[10001];vector p(10001);//가장 많은 간전을 가지고 있는 점을 저장함void BFS(int x){ queue q; q.push(x); while (!q.empty()) { int x = q.front(); ..
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1786번:찾기 답 KMP문제입니다//p는 찾으려는 문자열 //s는 주어지는 문자열/*KMP Algorithm -------------------------------------------------------------------------------------------1.시간 복잡도 O(n+m) - > pi배열을 만드는 시간 m(원하는 문자열의 길이), 주어진 문자열에서 원하는 문자열 찾는 시간 n2.Preprocessing - > pi배열 만들기 : 원하는 문자열은 p,주어진 문자열을 s라 했을 때,1)p의 접두사와, 접미사를 비교하여 p의 인덱스 마다 그 둘이 같은 것의 길이를 저장한다,같지 않으면 pi[i]=02)while문을 pi의 인덱스가 하나 증가할 때마다 돌린다(line 28) - > 현재 p[i]의 문자..
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 2133번:타일채우기 답 //3xn을 채우는데 1x2,2x1 타일로 총 세가지로 채울 수 있다.//가짓 수 : D[n] - > n이 짝수여야지만 주어진 타일로 채울 수 있다.//D[i] = 3*D[i-2]+2*D[i-4]+2*D[i-6]....+2*D[i-2*j]//D[i] = 3*D[i-2]+2*sum[i-4]라고 나타낼 수 있다.1234567891011121314#include using namespace std;int n,d[31];int main() { cin >> n; d[0] = 1; for (int i = 2; i
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1547번:공 답 123456789101112131415161718#include #include using namespace std;int main() { int t, s=0,a[4] = { 0,1,0,0 }; cin >> t; while (t--) { int x, y; cin >> x >> y; swap(a[x], a[y]); } for (int i = 1; i