Results 1 to 15 of 15

Thread: / C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User skrillex's Avatar
    Join Date: Feb:2012
    Location: Varna
    Posts: 137

    / C++

    !

    . : N (N <= 100), 'A', 'B' 'C' . . N = 6 : ABACAB, : AABACA - 'A' 'A'; ABCBCA - 'BC' 'BC' ABCABC 'ABC' 'ABC'.

    , , :

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <ctime>
    
    using namespace std;
    
    #define SIZE 1000
    
    static const char letters[] = "ABC";
    
    int len;
    
    /*
    char generate_rand()
    {
    
    	return letters[rand() % 3];
    
    }
    
    void generate_string()
    {
    
    	srand(time(0));
    	string str;
    	int n;
    
    	cout << "Enter n = ";
    	cin >> n;
    	cout << endl;
    	
    	for(int i = 0; i < n; i++)
    	{
    
    		str += generate_rand();
    
    	}
    
    	cout << "Generated string: " << str << endl << endl;
    
    	len = str.length();
    
    	cout << "String length: " << len << endl << endl;
    
    }
    */
    
    int main()
    {
    	char S[SIZE];
    	char repeat[SIZE];
    	int match = 0;
    	
    	cout << "Enter string: ";
    
    	fgets(S, SIZE-1, stdin);
    
    	int n = strlen(S);
    	
    	cout << "\nS = " << S; 
    	cout << "length =  " << n << endl;
    
    	for (int p = 1; p <= n/2; p++)
    	{
    		for (int i = 0; i < n-p; i++)
    		{
    
    			if (S[i] == S[i+p]) 
    			{
    
    				match++;
    				if (match >= p)
    				{
    					
    					strncpy(repeat, &S[i-p+1], 2*p);
    					repeat[2*p]='\0';
    
    					cout << "\nFound repeat of length " << 2*p << " at location " << i-p+1 << " is: " << repeat << endl;
    		
    				}
    			}
    			else match = 0;
    		}
    		
    	}
    
    	cout << endl;
    
    	system("pause");
    	return 0;
    }

  2. #2
    -==-
    Join Date: Sep:2003
    Location:
    Posts: 6,444
    . 7-8. . , .
    He who asks is a fool for five minutes, but he who does not ask reamins a fool forever. Old Chinese saying
    Quote Originally Posted by worm4 View Post
    a .

  3. #3
    Registered User skrillex's Avatar
    Join Date: Feb:2012
    Location: Varna
    Posts: 137
    Quote Originally Posted by Yasen6275 View Post
    . 7-8. . , .
    18 : ABCACBCABACBABCBAC
    , /2.

  4. #4
    -==-
    Join Date: Sep:2003
    Location:
    Posts: 6,444
    Quote Originally Posted by skrillex View Post
    18 : ABCACBCABACBABCBAC
    , /2.
    . . . . .

    BTW .

    // 5 .

    // 2^n. 7,11,15 2 .
    Last edited by Yasen6275; 29th March 2015 at 18:47.
    He who asks is a fool for five minutes, but he who does not ask reamins a fool forever. Old Chinese saying
    Quote Originally Posted by worm4 View Post
    a .

  5. #5
    Registered User skrillex's Avatar
    Join Date: Feb:2012
    Location: Varna
    Posts: 137
    Quote Originally Posted by Yasen6275 View Post
    . . . . .

    BTW .

    // 5 .

    // 2^n. 7,11,15 2 .
    , :

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    static const char letters[] = "ABC";
    
    char generate_rand()
    {
    
    	return letters[rand() % 3];
    
    }
    
    int check(char *s, int pos) 
    {
    
        for (int i = 1; i <= (pos + 1)/2; i++) 
    	{
    
    		int flag = 1;
    
    		for (int j = 0; j < i; j++)
    
    		if (s[pos-j] != s[pos-i-j]) 
    		{
    
    			flag = 0; 
    				continue;
    
            }
    
    		if (flag) 
    			return 1;
    
        }
    	return 0;
    }
     
    int main() 
    {
    	char s[100];
        int n;
    
    	cout<<"enter n: ";
    	cin>>n;
        
        srand(time(NULL));
    
        for (int i = 0; i < n; i++) 
    	{
    		do
    		{
    			s[i]=generate_rand(); 
    		} while (check(s, i));
    
            cout << s[i] << " ";
    
        }
    
        cout << " ok" << endl;
    
        cin.ignore(); 
        cin.get();
    
    	system("pause");
    	return 0;
    }
    , , . .

  6. #6
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    ,

    . N >= 8 ABACABA 7 , .

  7. #7
    Deleted User 4eRNoBiL's Avatar
    Join Date: Oct:2005
    Location:
    Posts: 739
    Quote Originally Posted by skrillex View Post
    !

    . : N (N <= 100), 'A', 'B' 'C' . . N = 6 : ABACAB, : AABACA - 'A' 'A'; ABCBCA - 'BC' 'BC' ABCABC 'ABC' 'ABC'.
    - , B, C, - , . , (), " ":
    Code:
    ([ABC])(?!\1)[ABC]
    Fujistu Lifebook E756 | Core i7-6500U / 400MHz-3.1GHz | 8 GB DDR4-2133 | Samsung PM871 / 256 GB SSD | 15" 1920x1080 | Manjaro Linux + kernel 4.19

  8. #8
    -==-
    Join Date: Sep:2003
    Location:
    Posts: 6,444
    Quote Originally Posted by 4eRNoBiL View Post
    - , B, C, - , . , (), " ":
    Code:
    ([ABC])(?!\1)[ABC]
    , , ?
    He who asks is a fool for five minutes, but he who does not ask reamins a fool forever. Old Chinese saying
    Quote Originally Posted by worm4 View Post
    a .

  9. #9
    Registered User ined's Avatar
    Join Date: Nov:2006
    Location: Varna
    Posts: 1,078
    , :
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    static const char letters[6][3] = {{'A','B','C'}, {'A','C','B'}, {'B','A','C'}, {'B','C','A'}, {'C','A','B'}, {'C','B','A'}};
    
    int check(char *s, int pos) {
        for (int i=1; i<=(pos+1)/2; i++) {
           int flag=1;
           for (int j=0; j<i; j++)
               if (s[pos-j]!=s[pos-i-j]) {
                   flag=0;
                   break;
               }
           if (flag) return 1;
        }
        return 0;
    }
    
    class letter
    {
    private:
        int seq, cval;
    public:
        letter()
        {
            seq = rand()%6;
            cval = 0;
        }
        char get()
        {
            return(letters[seq][cval]);
        }
        int next()
        {
            cval++;
            cval %= 3;
            return (cval);
        }
    };
    
    int main()
    {
        char string[101];
        letter  *seq;
        int clen = 0, req_len = 99;
        int i;
    
        srand(time(NULL));
        seq = new letter[req_len];
    
        string[req_len] = 0;
    
        while(clen < req_len) {
            string[clen] = seq[clen].get();
            if (!check(string, clen))
                clen++;
            else
                while(!seq[clen].next())
                    clen--;
        }
        
        delete[] seq;
        cout << "The string is:" << endl;
        cout << string << endl;
    }
    ASRock B85 Pro4, Pentium G3240, DD3 8GB/1333MHz, 120GB SSD + 1TB + 640GB HDD
    Gigabyte GV-R657OC-1GI, CM B500, ASUS 24B1S1, LCD SAMSUNG SM931BW, 173

  10. #10
    Deleted User 4eRNoBiL's Avatar
    Join Date: Oct:2005
    Location:
    Posts: 739
    Quote Originally Posted by ined View Post
    , ...
    ... . , - , . .

    Quote Originally Posted by Yasen6275
    , , ?
    , . , :
    1. , : - -
    2. , : ABAB -
    3. : ABCABC -

    , .

    , , , .
    : : "", "". : "", , .

    - , , .

    , , .
    Fujistu Lifebook E756 | Core i7-6500U / 400MHz-3.1GHz | 8 GB DDR4-2133 | Samsung PM871 / 256 GB SSD | 15" 1920x1080 | Manjaro Linux + kernel 4.19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |