U E D R , A S I H C RSS

문�열검색/허아�

소�

어쩌다가 �렇게 길어지게 �는지 모르겠다.
여튼 처� 문제를 접했� 때 연필� ��거리지 않았어서 후회가 �긴다.
연필� 잡지 않으면 시간� 배로 드는것 같다.
앞으로 실수하지 않게 �블스캠프때 배웠듯�, 건축� 있어서 설계를 작성하는것� 중요한 것 처럼
프로그래�� 있어서� 구�과 설계는 매우 중요하다고 �낀다.

알고리즘

그냥 알고리즘� ��했다. 어떻게 보면 복잡한 것 같기� 하다.ㅡㅠ
His teaching method is very good.
�번 프로그래�� 알고리즘.
단어� 시작� 중�으로 한다.(나는 문�열 ��기 보다는 단어 검색 중심으로 짠것같다.)
즉, H, t, m, i, v, g가 기�� � 수 있다.
문장 ì†�ì�˜ 단어ì�˜ 첫 글ìž�와, 찾아볼 단어ì�˜ 첫글ìž�ì�˜ ë�™ì�¼í•œê°€ ë€í„° 시작한다.
만약� Hot��는 단어를 저 문장�서 찾았�때, 먼저 H로 시작하는 단어가 있기 때문� His와 비�를 당하게 �다.
다�� o와 i.. 다르기 때문� found � 0� �어서 찾지 못했�� 알린다.
만약ì—� 첫 글ìž� ë€í„° ë�™ì�¼í•˜ì§€ 않다면 비êµ�ë�„ 하지 않고ë�„ 찾지못함ì�„ 알린다.

소스

~cpp 
#include <stdio.h>

void exist_word(char x[40], int exist_str[10]); //x[i]� 문�열� 유무
int compare_str(char x[40], char search_str[15], int exist_str[10]); // 문�열 비�

int word_num = 1, search_str_num = 0;
int found = 0;	int temp = 0;

void main()
{
	char x[40] = "His teaching method is very good.";
	char search_str[15];
	int exist_str[10]; // exist_str[i]�는 x문�열중 i번째 문�열� 몇번째 문�� 나오나 
	FILE *fp;
	fp = fopen("result.out", "w");
	while(1){
		word_num = 1; search_str_num = 0; found = 0; temp = 0;
		printf("�내려면 ""EE""입력\n"); //출력.
		printf("�료 -> %s", x);
		printf("\n찾� 문�열 -> ");
		scanf("%s", search_str);
		fprintf(fp,"�료 -> %s", x); //result.out� 저장.
		fprintf(fp, "\n찾� 문�열 -> ");
		fprintf(fp, "%s", search_str);
		if(search_str[0] == 'E' && search_str[1] == 'E')
			break;
		exist_word(x, exist_str);
		found = compare_str(x, search_str, exist_str);
		
		switch(found){
		case 1:
			fprintf(fp, "\nfirst found -> %d\n\n", exist_str[word_num]+1);
			break;
		case 0:
			fprintf(fp, "\nNot found!\n\n");
			break;
		}
	}
	fclose(fp);
}

void exist_word(char x[40], int exist_str[10])
{
	int num = 2, x_n = 0;
	exist_str[1] = 0;
	while(x[x_n])
	{
		if(x[x_n] == ' ')
		{
			exist_str[num] = x_n + 1; //빈칸 다�� 문�열� 나온다고 가정.
			++num;
		}
		++x_n;
	}
}

int compare_str(char x[40], char search_str[15], int exist_str[10])
{	
	if(x[exist_str[word_num]] != search_str[search_str_num])
	{
		++word_num;	
		if(exist_str[word_num] >= 0)
			compare_str(x, search_str, exist_str);
		
	}else{
		temp = exist_str[word_num];
		while(search_str[search_str_num])
		{	
			if(x[temp] == search_str[search_str_num]){
				found = 1;
				++temp;
				++search_str_num;
			}else{
				found = 0;
				break;
			}
		}
	}
	return found;
}

C++로 문� 검색.

~cpp 
//cpp1.cpp
#include <iostream>
#include "class.h"

using namespace std;

void main()
{
	Search_ch c;
	c.input();
	c.search();
	c.print();

}
~cpp 
//cpp2.cpp
#include <iostream>
#include "class.h"

using namespace std;

void Search_ch::input()
{
	cin>>str;
	cin>>ch;
}

int Search_ch::search()
{
	int i, str_len, k;
	str_len = strlen(str);

	for(i = 0; i <= str_len-1; i++)
	{
		if(str[i] == ch)
		{
			k = i;
			break;
		}else{
			k = 0;
		}
	}
	return k;
}

void Search_ch::print()
{
	int found_num = search();
	if(found_num == 0){
		cout << "Not Found!!";
	}else if(0 < found_num && found_num < strlen(str)){
		cout << "Found!! >>>" <<found_num+1; 
	}
}
~cpp 
//class.h
class Search_ch 
{
private:

public:
	char str[20];
	char ch;
	void input();
	int search();
	void print();

};

나한테 할 �

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:30
Processing time 0.0157 sec