por ejemplo si tengo la secuencia:
s=[1 (1), 0 (2), 1 (3), 1 (4), 0 (5), 0 (6), 1 (7), 0 (8), 0 (9), 1 (10) , 1 (11), 1 (12),
1 (13), 0 (14), 0 (15), 0 (16), 1 (17), 1 (18), 1 (19), 0 (20) ];
Los numeros entre parentesis son solo para indicar el orden.
Las subcadenas de longitud 4 serian:
01: 1 (01) 0 (02) 1 (03) 1 (04) -> [1,0,1,1],
02: 1 (01) 1 (03) 0 (05) 1 (07) -> [1,1,0,1],
03: 1 (01) 1 (04) 1 (07) 1 (10) -> [1,1,1,1],
04: 1 (01) 0 (05) 0 (09) 1 (13) -> [1,0,0,1],
05: 1 (01) 0 (06) 1 (11) 0 (16) -> [1,0,1,0],
06: 1 (01), 1 (07), 1 (13), 1 (19) -> [1,1,1,1],
07: 0 (02) 1 (03) 1 (04) 0 (05) -> [0,1,1,0],
08: 0 (02) 1 (04) 0 (06) 0 (08) -> [0,1,0,0],
09: 0 (02) 0 (05) 0 (08) 1 (11) -> [0,0,0,1],
10: 0 (02) 0 (06) 1 (10) 0 (14) -> [0,0,1,0],
11: 0 (02), 1 (07), 1 (12), 1 (17) -> [0,1,1,1],
12: 0 (02) 0 (08) 0 (14) 0 (20) -> [0,0,0,0],
13: 1 (03) 1 (04) 0 (05) 0 (06) -> [1,1,0,0],
14: 1 (03) 0 (05) 1 (07) 0 (09) -> [1,0,1,0],
15: 1 (03) 0 (06) 0 (09) 1 (12) -> [1,0,0,1],
16: 1 (03) 1 (07) 1 (11) 0 (15) -> [1,1,1,0],
17: 1 (03), 0 (08), 1 (13), 1 (18) -> [1,0,1,1],
18: 1 (04) 0 (05) 0 (06) 1 (07) -> [1,0,0,1],
19: 1 (04) 0 (06) 0 (08) 1 (10) -> [1,0,0,1],
20: 1 (04) 1 (07) 1 (10) 1 (13) -> [1,1,1,1],
21: 1 (04) 0 (08) 1 (12) 0 (16) -> [1,0,1,0],
22: 1 (04) 0 (09) 0 (14) 1 (19) -> [1,0,0,1],
23: 0 (05) 0 (06) 1 (07) 0 (08) -> [0,0,1,0],
24: 0 (05) 1 (07) 0 (09) 1 (11) -> [0,1,0,1],
25: 0 (05) 0 (08) 1 (11) 0 (14) -> [0,0,1,0],
26: 0 (05) 0 (09) 1 (13) 1 (17) -> [0,0,1,1],
27: 0 (05) 1 (10) 0 (15) 0 (20) -> [0,1,0,0],
28: 0 (06) 1 (07) 0 (08) 0 (09) -> [0,1,0,0],
29: 0 (06) 0 (08) 1 (10) 1 (12) -> [0,0,1,1],
30: 0 (06) 0 (09) 1 (12) 0 (15) -> [0,0,1,0],
31: 0 (06), 1 (10), 0 (14), 1 (18) -> [0,1,0,1],
32: 1 (07) 0 (08) 0 (09) 1 (10) -> [1,0,0,1],
33: 1 (07) 0 (09) 1 (11) 1 (13) -> [1,0,1,1],
34: 1 (07), 1 (10), 1 (13), 0 (16) -> [1,1,1,0],
35: 1 (07), 1 (11), 0 (15), 1 (19) -> [1,1,0,1],
36: 0 (08) 0 (09) 1 (10) 1 (11) -> [0,0,1,1],
37: 0 (08), 1 (10), 1 (12), 0 (14) -> [0,1,1,0],
38: 0 (08), 1 (11), 0 (14), 1 (17) -> [0,1,0,1],
39: 0 (08), 1 (12), 0 (16), 0 (20) -> [0,1,0,0],
40: 0 (09), 1 (10), 1 (11), 1 (12) -> [0,1,1,1],
41: 0 (09), 1 (11), 1 (13), 0 (15) -> [0,1,1,0],
42: 0 (09), 1 (12), 0 (15), 1 (18) -> [0,1,0,1],
43: 1 (10), 1 (11), 1 (12), 1 (13) -> [1,1,1,1],
44: 1 (10), 1 (12), 0 (14), 0 (16) -> [1,1,0,0],
45: 1 (10), 1 (13), 0 (16), 1 (19) -> [1,1,0,1],
46: 1 (11), 1 (12), 1 (13), 0 (14) -> [1,1,1,0],
47: 1 (11), 1 (13), 0 (15), 1 (17) -> [1,1,0,1],
48: 1 (11), 0 (14), 1 (17), 0 (20) -> [1,0,1,0],
49: 1 (12), 1 (13), 0 (14), 0 (15) -> [1,1,0,0],
50: 1 (12), 0 (14), 0 (16), 1 (18) -> [1,0,0,1],
51: 1 (13), 0 (14), 0 (15), 0 (16) -> [1,0,0,0],
52: 1 (13), 0 (15), 1 (17), 1 (19) -> [1,0,1,1],
53: 0 (14), 0 (15), 0 (16), 1 (17) -> [0,0,0,1],
54: 0 (14), 0 (16), 1 (18), 0 (20) -> [0,0,1,0],
55: 0 (15), 0 (16), 1 (17), 1 (18) -> [0,0,1,1],
56: 0 (16), 1 (17), 1 (18), 1 (19) -> [0,1,1,1],
57: 1 (17), 1 (18), 1 (19), 0 (20) -> [1,1,1,0],
y al final obtener un recuento de las veces que se repite cada patrón y su frecuencia relativa:
0 0 0 0 ------ 161.697 - 0,0606515378844711
0 0 0 1 ------ 163.593 - 0,0613627156789197
0 0 1 0 ------ 164.201 - 0,0615907726931733
0 0 1 1 ------ 166.680 - 0,0625206301575394
0 1 0 0 ------ 164.105 - 0,0615547636909227
0 1 0 1 ------ 166.501 - 0,0624534883720930
0 1 1 0 ------ 167.099 - 0,0626777944486122
0 1 1 1 ------ 168 835 - 0,0633289572393098
1 0 0 0 ------ 164.086 - 0,0615476369092273
1 0 0 1 ------ 166.963 - 0,0626267816954239
1 0 1 0 ------ 166.931 - 0,0626147786946737
1 0 1 1 ------ 169 470 - 0,0635671417854464
1 1 0 0 ------ 166.622 - 0,0624988747186797
1 1 0 1 ------ 169.326 - 0,0635131282820705
1 1 1 0 ------ 169 251 - 0,0634849962490623
1 1 1 1 ------ 170 640 - * 0,0640060015003751
Para ello intento utilizar el siguiente codigo:
Código
#include <iostream> #include <fstream> #include <stdlib.h> #include <vector> #include <string> using namespace std; int main() { string cadena; char pr; // ofstream fo ("entreno.txt"); ifstream fi("entreno.txt"); int numero; int patron_int; } // fo.close(); getline(fi, cadena); vector<int>v(cadena.size()); vector<int>patron(4); vector<long long>patrones(16); float N; //cout << cadena; //numero = Int32.Parse(cadena[0]); //pr=cadena[0].c_str(); //cout << numero; //cout << cadena[0]; for(int i=0; i<cadena.size(); i++) { // if (cadena[i]=='1') v[i]=0; // else if (cadena[i]=='1') v[i]=1; v[i]=cadena[i]-48; // cout << v[i]; } for(int i=0;i<v.size()-3;i++) { for(int j=i;j<v.size()-3;j++) { patron[3]=v[i]; patron[2]=v[j+1]; patron[1]=v[j+2]; patron[0]=v[j+3]; //cout << patron[3] << patron[2] << patron[1] << patron[0] << endl; patron_int= patron[0] + patron[1]*2 + patron[2]*4 + patron[3]*8; //cout << patron_int << endl; patrones[patron_int]++; //if(i==v.size()/5) system("pause"); //system("pause"); } N=(100*i)/(v.size()-3); // cout << N << "%" << endl; } for(int i=0; i<patrones.size(); i++) { cout << i << " se repite " << patrones[i] << " veces." << endl; } //cout << v.size(); system("pause"); fi.close(); return 0; }
Pero debido a que no soy un programador experto en C++, quisiera mejorarlo y modificarlo de forma que me calculara subcuerdas de 4-20.
Primero me podrías decir que debo modificar y despues os pregunto dudas para asi intentar aprender a programar en C++.
Muchas gracias