Example #1
0
    vector<string> generatePalindromes(string s) {
        vector<string> ans;
        if (s.length() == 1) ans.push_back(s); 
        if (s.length() <= 1) return ans;

        const int SIZE = 256;
        int ct[SIZE] = {0}, len = s.length();
        for (int i = 0; i < len; ++ i) ++ ct[s[i]];

        // count number of each char, 
        // and get the index of the char of odd occurrence.
        int odd = 0, odd_index = 0;
        for (int i = 0; i < SIZE; ++ i) {
            if (ct[i] & 1) {
                odd ++;
                odd_index = i;
            }
        }
        
        // iff can have palindromes, get such strings.
        if (((len & 1 == 1) && odd == 1) || (odd == 0)) {
            vector<int> nums = getChars(ct, SIZE); // get half of the chars.
            sort(nums.begin(), nums.end());
            
            do {
                ans.push_back(getPalindrome(nums, len & 1 == 1, odd_index));
            } while (nextPermutation(nums));
        }
        
        return ans;
    }
int main()
{
    FILE *fin=fopen("pprime.in","r");
    FILE *fout=fopen("pprime.out","w");
    fscanf(fin,"%d%d",&a,&b);
    if(a<100)
    {
        if(a<=5)fprintf(fout,"5\n");
        if(a<=7)fprintf(fout,"7\n");
        if(a<=11)fprintf(fout,"11\n");
        a=101;
        if(b<100)
        {
            b=101;
        }
    }
    int num=initPalindrome(a);
    while(true)
    {
        int temp=getPalindrome(num);
        if(temp<a or temp>b)
        {
            break;
        }
        if(isPrime(temp))
        {
            fprintf(fout,"%d\n",temp);
        }
        num++;
    }
    return 0;
}
Example #3
0
void Menu::executePALINDROME() {
	int nMin = 0, nMax = 0;
	cout << "\tPalindrome 수 구하기 함수를 선택하셨습니다. 범위를 입력해주세요." << endl;
	cout << "- 최소값 : ";
	cin >> nMin;

	cout << "- 최소값 : ";
	cin >> nMax;

	int cnt = getPalindrome(nMin, nMax);

	cout << "▶ 범위 " << nMin << " ~ " << nMax << " 사이의 Palindrome 수의 개수는 " << cnt << "개 입니다." << endl;
}
Example #4
0
/**
 * function: longestPalindrome
 * description: for a given string 'str' return the longest palindrome string
 *              that is its subset.
**/
void longestPalindrome(char *str, char *palindrome) {
    /* 1. check input arguments */
    if (str == NULL) { return; }

    int maxstart, maxlen;
    int center, start, end, len;

    bzero(palindrome, MAXLEN); /* zero out the output buffer */
    maxstart = 0; maxlen = 1;  /* initial palindrome: first letter */

    /* check if each position in str is the center of a palindrome */
    for (center = 0; center < strlen(str) - 1; center++) {
        /* get for odd-length palindrome */
        getPalindrome(str, center, center, &start, &end);

        /* save info if this is the longest palindrome yet */
        len = end - start + 1;
        if (len > maxlen) {
            maxlen = len;
            maxstart = start;
        }

        /* get for even-length palindrome (hint: it has two centers} */
        if (str[center] == str[center+1]) {
            getPalindrome(str, center, center+1, &start, &end);
            /* save info if this is the longest palindrome yet */
            len = end - start + 1;
            if (len > maxlen) {
                maxlen = len;
                maxstart = start;
            }
        }
    }

    /* copy longest found palindrome to the output buffer */
    strncpy(palindrome, str+maxstart, maxlen);
}
 int largestPalindrome(int n) {
     if (n == 1) return 9;
     uint64_t max_n = 1;
     for (int i = 0; i < n; ++i) {
       max_n *= 10;
     }
     max_n--;
     uint64_t min_n = (max_n + 1) / 10;
     for (uint64_t a = max_n; a >= min_n; --a) {
       uint64_t u = getPalindrome(a, n);
       //printf("try u = %llu\n", (unsigned long long)u);
       double limit = sqrt(u);
       for (uint64_t b = max_n; b >= limit; b--) {
         if (u % b == 0) {
           //printf("b = %u\n", (int)b);
           return u % 1337;
         }
       }
     }
 }
int initPalindrome(int number)
{
    char s[100];
    sprintf(s,"%d",number);
    int len=(strlen(s)+1)/2;
    int res=0;
    for(int i=0;i<len;i++)
    {
        res=res*10+s[i]-'0';
    }
    while(true)
    {
        int temp=getPalindrome(res);
        if(temp>=a and temp<=b)
        {
            return res;
        }
        res++;
    }
}