2015 public comments on campus recruitment pen questions-compare the same and different parts of the two strings, 2015 campus recruitment

Source: Internet
Author: User

2015 public comments on campus recruitment pen questions-compare the same and different parts of the two strings, 2015 campus recruitment

Implementation using an array hash table </span>
// String str1, string str2, str1 length n1, str2 length n2, res1 storage same character, res2 storage different character void charcmp (char * str1, char * str2, int n1, int n2, char * res1, char * res2) {int a1 [60] = {0}; // use a hash array to record the number of occurrences of str1 characters, there are a total of 52 uppercase and lowercase letters. The ascii alphabet contains 6 Other symbols: int a2 [60] = {0}; // records the number of occurrences of str2 characters using a hash array, there are a total of 52 uppercase and lowercase letters. The ascii alphabet contains 6 Other symbols for (int I = 0; I <n1; I ++) {a1 [str1 [I]-'a'] ++; // use str [0] to record the number of times of A} for (int I = 0; I <n2; I ++) {a2 [str2 [I]-'a'] ++;} int k = 0; int m1 = 0; int m2 = 0; for (; k <60; k ++) {if (a1 [k]! = 0 | a2 [k]! = 0) {if (a1 [k]! = 0 & a2 [k]! = 0) // The letter {res1 [m1 ++] = k + 'A' that appears in both strings; // The number is converted to A character, K = 0 indicates the number of times A is stored, corresponds to character A} else // only the characters in the string {res2 [m2 ++] = k + 'A ';}}} res1 [m1] = '\ 0'; // Add the terminator res2 [m2] =' \ 0'; // Add the terminator} int main () {char str1 [] = "abcd"; char str2 [] = "fagh"; char res1 [5] = {'\ 0 '}; char res2 [10] = {'\ 0'}; charcmp (str1, str2, 4, res1, res2); getchar (); return 1 ;}




Related Article

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.