반응형

App.vue

<template>
  <div>
    <GreetingUser id="greeting"></GreetingUser>
  </div>
</template>
<script>
import GreetingUser from "./components/GreetingUser";
export default {
  name: "App",
  components: {
    GreetingUser,
    // ProductList,
  },
  data() {
    return {
      // products: [
      //   { id: 0, name: "TV", price: 500000, company: "LG" },
      //   { id: 1, name: "전자렌지", price: 200000, company: "삼성" },
      //   { id: 2, name: "가스오븐", price: 300000, company: "한화" },
      //   { id: 3, name: "냉장고", price: 50000, company: "대우" },
      //   { id: 4, name: "에어컨", price: 1000000, company: "해태" },
      // ],
    };
  },
  methods: {},
};
</script>

<style></style>

 

GreetingUser.vue

<template>
  <div>
    <h2 v-bind="$attrs">Hello {{ username }}</h2>
    <h3 v-bind="$attrs">오늘이 {{ numberOfVisit }} 번째 방문입니다.</h3>
    <h3>사이트 이름은 {{ siteInfo.name }}입니다</h3>
  </div>
</template>
<script>
export default {
  name: "GreetingUser",
  inheritAttrs: false,
  props: {
    //username: String,
    username: {
      type: String,
      default: "user!",
    },
    numberOfVisit: {
      type: Number,
      default: 0,
    },
    siteInfo: {
      type: Object,
      default: () => {
        return { name: "-", teacher: "-" };
      },
    },
  },
};
</script>
<style scoped></style>

 

 

ProductList.vue

<template>
  <div>
    <ul>
      <ProductItem
        v-for="product in products"
        :key="product.id"
        :product="product"
      ></ProductItem>
    </ul>
    <ProductItem />
  </div>
</template>

<script>
import ProductItem from "./ProductItem";

export default {
  name: "product-list",
  components: {
    ProductItem,
  },
  props: {
    products: {
      type: Array,
      default() {
        return [];
      },
    },
  },
};
</script>

<style></style>

 

 

 

ProductItem.vue

<template>
  <li>
    <p class="title">{{ product.name }}</p>
    <p>
      <img
        :src="`https://placeimg.com/200/100/${product.id}`"
        :alt="product.name"
      />
    </p>
    <p class="price">{{ product.price }}</p>
  </li>
</template>

<script>
export default {
  name: "product-item",
  props: {
    product: {
      type: Object,
      default: () => {
        return { id: 0, name: "TV", price: 500000, company: "LG" };
      },
    },
  },
};
</script>

<style scoped>
li {
  border: 2px solid salmon;
  margin-bottom: 0.5rem;
}

.title {
  font-size: 24px;
  color: #2c82c9;
  font-weight: bold;
}
</style>

 

 

vue Component 사용법

1. import

2. components object 추가

3. props value 넘겨줌

4. 컴포넌트에서 props 받아서 처리

 

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] Component5 , keep-alive, dynamic component  (0) 2022.06.29
[Vue] Component4, your input is  (0) 2022.06.29
[Vue] Component 2  (0) 2022.06.25
[Vue] Component 1  (0) 2022.06.25
[Vue] Watch  (0) 2022.06.25
반응형

https://www.youtube.com/watch?v=j_sdjivoPs8 

#include <bits/stdc++.h>
using namespace std;

int arr[5][5];
int dp[5][5];
char P[5][5];


void path(int x, int y){
    if(x==1 && y==1){
        cout << x << ' ' << y << '\n';
        return;
    }
    else if(P[x][y]=='^'){
        path(x-1,y);
    }
    else if(P[x][y]=='<'){
        path(x,y-1);
    }

    cout << x << ' ' << y << '\n';
    return;
}


int main(){
    ios::sync_with_stdio(0); cin.tie(0);

    arr[1][1] = 6;
    arr[1][2] = 7;
    arr[1][3] = 12;
    arr[1][4] = 5;
    arr[2][1] = 5;
    arr[2][2] = 3;
    arr[2][3] = 11;
    arr[2][4] = 18;
    arr[3][1] = 7;
    arr[3][2] = 17;
    arr[3][3] = 3;
    arr[3][4] = 3;
    arr[4][1] = 8;
    arr[4][2] = 10;
    arr[4][3] = 14;
    arr[4][4] = 9;



    for(int i=1; i<5; i++){
        for(int j=1; j<5; j++){
            cout << arr[i][j] << ' ';
        }
        cout << '\n';
    }

    for(int i=1; i<5; i++){
        for(int j=1; j<5; j++){
            if(i==1 && j==1) dp[i][j] = arr[i][j];
            else if(i==1) dp[i][j] = arr[i][j] + dp[i][j-1];
            else if(j==1) dp[i][j] = arr[i][j] + dp[i-1][j];
            else dp[i][j] = arr[i][j] + min(dp[i-1][j], dp[i][j-1]);
        }
    }

    cout << '\n';

    for(int i=1; i<5; i++){
        for(int j=1; j<5; j++){
            cout << dp[i][j] << ' ';
        }
        cout << '\n';
    }


    int x=4, y=4;



    for(int i=1; i<5; i++){
        for(int j=1; j<5; j++){
            if(i==1 && j==1) P[i][j] = '-';
            else if(dp[i-1][j] < dp[i][j-1] || j==1){
                P[i][j] = '^';
            }
            else{
                P[i][j] = '<';
            }
        }
    }

    cout << '\n';

    for(int i=1; i<5; i++){
        for(int j=1; j<5; j++){
            cout << P[i][j] << ' ';
        }
        cout << '\n';
    }


    path(4, 4);

    return 0;
}
반응형
반응형

https://www.acmicpc.net/problem/2401

 

2401번: 최대 문자열 붙여넣기

어떤 긴 문자열이 주어지고 여러 개의 짧은 문자열들이 주어질때 이때 짧은 문자열을 긴 문자열에 붙여 넣을때 가장 길게 붙여 넣는 경우를 찾아라. 단 이때 짧은 문자열들끼리는 교차 할 수 없

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;
#define LEN_MAX 100003

string shortStr[501];
int lps[501][10001];
int mem[501];

int dp[LEN_MAX];

int main(){
    ios::sync_with_stdio(0); cin.tie(0);

    string longStr;
    int n, Len;
    cin >> longStr >> n;
    Len = longStr.length();

    //BFS 좌표 저장
    for(int N=0; N<n; N++){
        cin >> shortStr[N];

        int sLen = shortStr[N].length();
        for(int i=1, j=0; i<sLen; i++){
            while(j>0 && shortStr[N][i]!=shortStr[N][j]){
                j = lps[N][j-1];
            }
            if(shortStr[N][i]==shortStr[N][j]){
                lps[N][i] = ++j;
            }
        }
    }

    
    
    for(int i=0; i<Len; i++){
        if(i>0) dp[i] = dp[i-1];

        for(int j=0; j<n; j++){
            int sLen = shortStr[j].length();

            while(mem[j]>0 && longStr[i]!=shortStr[j][mem[j]]){
                mem[j] = lps[j][mem[j] - 1];
            }

            if(longStr[i]==shortStr[j][mem[j]]){
                if(mem[j] == sLen - 1){
                    //dp[-1] 방 지
                    int tmp = i -sLen >= 0 ? dp[i-sLen] : 0;
                    tmp += sLen;
                    dp[i] = max(dp[i], tmp);

                    mem[j] = lps[j][mem[j]];
                }
                else{
                    mem[j]++;
                }
            }

        }
    }

    cout << dp[Len-1] << '\n';

    return 0;
}

 

반응형
반응형

https://www.acmicpc.net/problem/4354

 

4354번: 문자열 제곱

알파벳 소문자로 이루어진 두 문자열 a와 b가 주어졌을 때, a*b는 두 문자열을 이어붙이는 것을 뜻한다. 예를 들어, a="abc", b="def"일 때, a*b="abcdef"이다. 이러한 이어 붙이는 것을 곱셈으로 생각한다

www.acmicpc.net

1.문제설명

 

KMP 알고리즘에 대해 공부하고 예제를 풀던 중

백준 4354번 문자열 제곱 문제를 풀게 되었다.

 

처음에 이 문제를 어떻게 KMP 알고리즘으로 풀 수 있는지 한참 고민했으나

생각이 나지 않아 구글에 여러 블로그를 뒤져보아도

마땅히 명쾌한 설명을 발견하지 못해 더 한참을 고민했다.

 

 

그러다 유튜브를 뒤져보던 중

'개발자영맨' 님의 동영상에서 다음과 같은 KMP 알고리즘의 성질을 발견했다.

말로 설명해주시는 걸 들으면 쉽게 이해할 수 있어서

동영상 31분부터 보면 좋을 것 같다.

 

KMP prefix 특성

출처: https://www.youtube.com/watch?v=OhMFeV8VeAc&t=510s 

 

 

 

실패함수든 lps든 prefix function이든

KMP알고리즘을 사용하기위해

prefix와 suffix를 구하면 그를 통해 반복 패턴의 길이를 구해낼 수 있다.

 

 

즉 이 백준문제에서는 단순히 답을 구하기위해

전체 string의 길이에서 위에서 구한 반복 패턴의 길이를 나누었을 때 나머지가 0으로 나누어 떨어진다면

(답 = 전체 string의 길이 / 반복패턴의 길이) 가 된다.

 

 

 

좀 더 자세히 설명하자면 위의 사진과 동일하게

pattern 문자열 = abababa 로 생각해보면

 

실패함수[n-1] = 5 이고,

전체 string의 길이 = 7 이므로

반복 패턴의 길이는 2 이고

반복 패턴은 ab이며

 

pattern 문자열(abababa)은 반복 패턴 ab가 반복된 모양이 된다.

즉 KMP 알고리즘에서 실패함수를 유도하는 과정에서 pattern에 대한 반복 패턴도 자연스럽게 얻어진다.

 

이러한 성질을 통해 코드를 구현하면 된다.

 

 

 


 

 

2.문제풀이코드

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(0); cin.tie(0);

    string s;
    while(1){
        cin >> s;
        if(s==".") break;

        int N = s.length();
        int lps[N];
        memset(lps,0,sizeof(lps));

        //prefix function
        int j = 0;
        for(int i=1; i<N; i++){
            while(j>0 && s[i]!=s[j]){
                j = lps[j-1];
            }
            if(s[i]==s[j]){
                j++;
                lps[i] = j;
            }
        }

        int repeatLength = N - lps[N-1];
        if(N % repeatLength == 0){
            cout << N / repeatLength << '\n';
        }
        else{
            cout << 1 << '\n';
        }
    }

    return 0;
}

백준 4354번

 

반응형
반응형

GreetingUser.vue

<template>
  <h2>Hello {{ username }}</h2>
  <h3>오늘이 {{ numberOfVisit }} 번째 방문입니다.</h3>
  <h3>사이트 이름은 {{ siteInfo.name }}입니다</h3>
</template>
<script>
export default {
  name: "GreetingUser",
  props: {
    //username: String,
    username: {
      type: String,
      default: "user!",
    },
    numberOfVisit: {
      type: Number,
      default: 0,
    },
    siteInfo: {
      type: Object,
      default: () => {
        return { name: "-", teacher: "-" };
      },
    },
  },
};
</script>
<style scoped></style>

 

App.vue

<template>
  <div>
    <!-- 데이터를 자식 컴포넌트에서 바꿀 수 없다. -->
    <GreetingUser
      :username="username"
      :numberOfVisit="numberOfVisit"
      :siteInfo="siteInfo"
    />
    <GreetingUser username="abcd" />

    <GreetingUser />
    <button @click="changeName()">change</button>
  </div>
</template>
<script>
import GreetingUser from "./components/GreetingUser.vue";
export default {
  name: "App",
  components: { GreetingUser },
  data() {
    return {
      username: "scalper",
      numberOfVisit: 25,
      siteInfo: {
        name: "vue practice",
        teacher: "scalper",
        subject: "frontend",
      },
    };
  },
  methods: {
    changeName() {
      this.username = "Ddochi";
    },
  },
};
</script>

<style></style>

 

 

 

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] Component4, your input is  (0) 2022.06.29
[Vue] Component 3  (0) 2022.06.29
[Vue] Component 1  (0) 2022.06.25
[Vue] Watch  (0) 2022.06.25
[Vue] Computed  (0) 2022.06.25
반응형

GreetingUser.vue

<template>
  <h2>Hello {{ username }}</h2>
</template>
<script>
export default {
  name: "GreetingUser",
  props: {
    //username: String,
    username: {
      type: String,
      default: "user!",
    },
  },
};
</script>
<style scoped></style>

 

 

App.vue

<template>
  <div>
    <!-- 데이터를 자식 컴포넌트에서 바꿀 수 없다. -->
    <GreetingUser :username="username" />
    <GreetingUser username="abcd" />
    <GreetingUser username="pororo" />
    <GreetingUser username="monkey" />
    <GreetingUser />
    <button @click="changeName()">change</button>
  </div>
</template>
<script>
import GreetingUser from "./components/GreetingUser.vue";
export default {
  name: "App",
  components: { GreetingUser },
  data() {
    return {
      username: "scalper",
    };
  },
  methods: {
    changeName() {
      this.username = "Ddochi";
    },
  },
};
</script>

<style></style>

 

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] Component 3  (0) 2022.06.29
[Vue] Component 2  (0) 2022.06.25
[Vue] Watch  (0) 2022.06.25
[Vue] Computed  (0) 2022.06.25
[Vue] Directives  (0) 2022.06.24
반응형
<template>
  <div>
    <h1>watchers</h1>
    <h1>current money :: {{ money }}</h1>
    <div>
      <button @click="money += 100">earn money</button>
      <button @click="money -= 100">spend money</button>
    </div>
    <h3>{{ receipt }}</h3>
    <button @click="receipt.food += 500">buy food</button>
    <hr />
    <input type="text" v-model="userName" />
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      userName: "scalper",
      money: 0,
      receipt: {
        food: 3000,
        fee: 2000,
        fare: 10000,
      },
    };
  },
  watch: {
    //오브젝트의 경우 다르게 해야함
    // receipt(newValue, oldValue) {
    //   console.log("영수증에 값 변화가 있음", newValue, oldValue);
    // },
    userName: {
      handler(newValue) {
        console.log(newValue, "newValue");
      },
      immediate: true,
    },

    receipt: {
      handler(newValue) {
        console.log("영수증에 값 변화", newValue);
      },
      deep: true,
    },
    money(newValue, oldValue) {
      console.log(oldValue);
      if (newValue > 2000 && newValue > oldValue) {
        console.log("mission complete");
      }

      if (oldValue < 1500 && newValue < oldValue) {
        console.log("save money!");
      }
    },
  },
};
</script>

<style>
a {
  font-size: 24px;
  display: block;
}

label {
  font-size: 22px;
  font-weight: bold;
  margin-right: 1 rem;
}

div {
  margin-bottom: 1rem;
}
</style>

watch는 값의 변화를 추적할 수 있다.

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] Component 2  (0) 2022.06.25
[Vue] Component 1  (0) 2022.06.25
[Vue] Computed  (0) 2022.06.25
[Vue] Directives  (0) 2022.06.24
[Vue] 폼 핸들링  (0) 2022.06.24
반응형
<template>
  <div>
    <h1>Computed</h1>
    <h2>{{ address }}</h2>
    <h2>내가 받은 점수의 합 {{ totalScore }}</h2>
    <h2>method : {{ getTotalScore() }}</h2>
    영어점수<input type="number" v-model="grade.eng" /> 학생이름<input
      type="text"
      v-model="studentName"
    />
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      username: "scalper",
      address1: "성남시",
      address2: "분당구",
      address3: "정자로",
      grade: {
        math: 70,
        kor: 90,
        eng: 50,
        sci: 55,
      },
      studentName: "",
    };
  },
  computed: {
    //computed는 cache가 된다 !!
    address() {
      return `${this.address1} - ${this.address2} - ${this.address3}`;
    },
    totalScore() {
      console.log("computed!!");
      // destructuring
      const { math, kor, eng, sci } = this.grade;
      return math + kor + eng + sci;
    },
  },
  methods: {
    //메서드는 화면에 변동이 있을 때마다 계산을 한다!
    getTotalScore() {
      console.log("methods!!");
      const { math, kor, eng, sci } = this.grade;
      return math + kor + eng + sci;
    },
  },
};
</script>

<style>
a {
  font-size: 24px;
  display: block;
}

label {
  font-size: 22px;
  font-weight: bold;
  margin-right: 1 rem;
}

div {
  margin-bottom: 1rem;
}
</style>

Computed는 간략한 계산

값 캐시효과

 

반응형

'Web > Vue' 카테고리의 다른 글

[Vue] Component 1  (0) 2022.06.25
[Vue] Watch  (0) 2022.06.25
[Vue] Directives  (0) 2022.06.24
[Vue] 폼 핸들링  (0) 2022.06.24
[Vue] 이벤트  (0) 2022.06.24

+ Recent posts