Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include<locale>
- char GetLower(char c) {
- if (c >= 'A' && c <= 'Z')
- return c + (int('a') - int('A'));
- if (c >= 'А' && c <= 'Я')
- return c + 32;
- if (c == 'Ё')
- return 'ё';
- return c;
- }
- bool is_invis(char c) {
- return c < 10 && c >= 0;
- }
- bool is_sign(char c) {
- return c == ',' || c == '.' || c == ';' || c == ':';
- }
- int calclen(char* word) {
- int sz = 0;
- while (word && !is_invis(word[0]) && !is_sign(word[0])) {
- sz++;
- word++;
- }
- return sz;
- }
- bool KMP(char* w1, char* w2) {
- if (calclen(w1) != calclen(w2))
- return false;
- for (int k = 0; k < calclen(w1); k++)
- if (GetLower(w1[k]) != GetLower(w2[k])
- && !is_sign(w1[k])
- && !is_sign(w2[k])
- )
- return false;
- return true;
- }
- void push_back(int* arr, int val, int size) {
- for (int i = 0; i < size; i++) {
- if (arr[i] == -1) {
- arr[i] = val;
- break;
- }
- }
- }
- int main() {
- FILE *S1, *S2;
- S1 = fopen("S1.txt", "r");
- S2 = fopen("S2.txt", "w");
- char words[10000][100];
- int words_cnt = 0;
- fscanf(S1, "%[^ ]", &words[words_cnt++]);
- while (fgetc(S1) != EOF)
- fscanf(S1, "%[^ ]", &words[words_cnt++]);
- int i = 1;
- while (words[i - 1][strlen(words[i - 1]) - 1] != '.')
- i++;
- int* words_c = (int*)malloc(sizeof(int)*i);
- memset(words_c, 0, sizeof(int)*i);
- for (int j = i; j < words_cnt; j++) {
- for (int l = 0; l < i; l++) {
- if (KMP(words[j], words[l]))
- words_c[l]++;
- }
- }
- int** strings_ids = (int**)malloc(sizeof(int*)*i);
- for (int k = 0; k < i; k++) {
- strings_ids[k] = (int*)malloc(sizeof(int)*words_c[k]);
- for (int l = 0; l < words_c[k]; l++)
- strings_ids[k][l] = -1;
- }
- int sentence = 2;
- for (int j = i; j < words_cnt; j++) {
- for (int l = 0; l < i; l++) {
- if (KMP(words[j], words[l]))
- push_back(strings_ids[l], sentence, words_c[l]);
- }
- if (words[j][strlen(words[j]) - 1] == '.')
- sentence++;
- }
- for (int j = 0; j < i; j++)
- {
- int old = 0;
- if (j == i - 1) words[j][strlen(words[j])- 1] = ' ';
- fprintf(S2, "%s\n", words[j]);
- fprintf(S2, "%d\n", words_c[j]);
- for (int k = 0; k < words_c[j]; k++) {
- if (old == strings_ids[j][k])
- continue;
- fprintf(S2, "%d ", strings_ids[j][k]);
- old = strings_ids[j][k];
- }
- fprintf(S2, "\n");
- }
- fclose(S1);
- fclose(S2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement