Algorithm title: UVA 1326 Jurassic Remains (Midway encounter method)

Source: Internet
Author: User
Tags bool printf time limit uppercase letter

The main effect of the topic:

To a string of n capitals, select as many strings as possible so that each uppercase letter can appear even several times.

Ideas:

At the sight of time limit:18.000 seconds, very high without any optimization of direct violence wrote one, more than 9s, estimated to be the longest time in his history of AC embarrassment

Then think about how to optimize, found that all letters appear in the number can be expressed in binary, 0 for even, 1 for odd. In this case, all the selected string states are performed or calculated once, and the result of 0 indicates that all are even.

So it's down from 9s to 1.692s.

The competition guide introduces more efficient "halfway encounters": dividing a string into 2 parts, first calculating the XOR value of all the combinations of the previous N/2 strings, saving them in the set map, and then n/2 the characters after the enumeration, looking for the same value as before.

//UVA 1326 Jurassic remains//Direct bit operation compression #include <cstdio> #include <cstring> #include <iostr  
    
Eam> #include <cctype> using namespace std;  
int n;  
Char str[30];  
int st[30];  
    
    
BOOL Vis[30];  
        int dfs (int cur, int cnt, int sta) {if (cur==n) {if (!sta) return CNT;  
    return-1;  
        } if (cur<n) {vis[cur] = true;  
        int res = DFS (cur+1, cnt+1, Sta^st[cur]);  
    
        if (res!=-1) return res;  
        Vis[cur] = false;  
        res = DFS (cur+1, CNT, STA);  
    if (res!=-1) return res;  
        int main () {while (~SCANF ("%d", &n)) {memset (st, 0, sizeof (ST));  
            for (int i=0; i<n; ++i) {scanf ("%s", str);  
            for (int j=0; str[j]; ++j) {st[i] ^= (1<< (str[j]-' A '));  
        } memset (Vis, 0, sizeof (VIS)); printf ("%d\n", DFS (0, 0, 0));  
        BOOL First=true;  
            for (int i=0; i<n; ++i) if (Vis[i]) {A-I first=false:putchar (");  
        printf ("%d", i+1);  
    } putchar (' \ n ');  
return 0; }

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.