Ultraviolet A 353-Pesky palindromes

Source: Internet
Author: User

Question: count the number of input substrings of a string.

Analysis: DP, brute force. Because the data is small, you can solve the problem directly.

Note: (the first 800 has been received by the attacker ).

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;char str[82];char ans[3200][82];int main(){while (~scanf("%s",str)) {int count = 0,len = strlen(str);for (int i = 1 ; i <= len ; ++ i)for (int s = 0 ; s < len ; ++ s) {int flag = 1;for (int t = s+i-1 ; t >= s ; -- t)if (str[s+s+i-1-t] != str[t]) {flag = 0;break;}if (flag) {for (int j = 0 ; j < i; ++ j)ans[count][j] = str[s+j];ans[count][i] = 0;int same = 0;for (int j = 0 ; j < count ; ++ j) if (!strcmp(ans[j], ans[count])) {same = 1;break;}if (!same) count ++;}}printf("The string \'%s\' contains %d palindromes.\n",str,count);/*printf("The %d unique palindromes in \'boy\' are",count);for (int i = 0 ; i < count-1 ; ++ i) {printf(" \'%s\'",ans[i]);if (i < count-2)printf(",");else printf(" and ");}printf("\'%s\'.\n\n",ans[count-1]);*/}return 0;}

Ultraviolet A 353-Pesky palindromes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.