Topcoder SRM 630div 2

Source: Internet
Author: User

A: eliminate two adjacent equal characters.

I really don't like STL ..

#include<iostream>#include <string>#include <vector>using namespace std;class DoubleLetter {    public:    string ableToSolve(string S) {    while (1){    int flag=0;    for (int i=1;i<S.size();i++)    if (S[i]==S[i-1])    {       S.erase(i-1,2);       flag=1;    }    if (!flag||S.size()==0) break;    }    if (S.size()==0) return "Possible";    else return "Impossible";    }};// Powered by FileEdit// Powered by TZTester 1.01 [25-Feb-2003]// Powered by CodeProcessor

B: Give a tree with N nodes, and find the maximum m value (n <= 10) at the same distance between the points in m pairs );

First, Floyd finds the distance between all vertices.

Then, the solution is enumerated in a way similar to the State compressed DP.

Then compare the maximum value.

#include<iostream>#include <string>#include <vector>#include<string.h>using namespace std;int b[12345];int dis[12][12];int mp[12][12];class Egalitarianism3Easy {    public:    int maxCities(int n, vector <int> a, vector <int> b, vector <int> len) {    memset(dis,0x3f3f3f,sizeof(dis));        for (int i=1;i<=n;i++) dis[i][i]=0;    for (int i=0;i<a.size();i++) dis[a[i]][b[i]]=dis[b[i]][a[i]]=len[i];        for (int k=1;k<=n;k++)        for (int i=1;i<=n;i++)        for (int j=1;j<=n;j++)        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);                int Max=1;        for (int i=1;i<(1<<n);i++)        {            vector<int> V;            V.clear();            for (int j=0;j<n;j++)            if (((i>>j)&1)==1) V.push_back(j+1);                        int m=V.size();            int same=dis[V[0]][V[m-1]];            int flag=1;                       for (int k=0;k<m;k++)                for (int j=0;j<m;j++)                if (k!=j&&dis[V[k]][V[j]]!=same)                {                flag=0;                break;            }           if (flag) Max=max(Max,m);        }        return Max;    }};

C: Exercise your thinking.

Reference others' practices:

Use an array similar to a suffix.

It's just that the array sets are really drunk.

My solution is to find the minimum string that satisfies the same sequence number as s, and then compare it with the value of S.

Detailed comments are provided in the Code.

# Include <iostream> # include <string> # include <vector> # include <string. h ># include <algorithm> using namespace STD; Class suffixarraydiv2 {public: String arr [100]; int P [100], a [100]; // note p: indicates the subscript of the original array after the string is sorted. // A: indicates the subscript of the original string after sorting. // This is very round, char C [100]; string smallerone (string s) {for (INT I = 0; I <S. size (); I ++) Arr [I] = & S [I]; // Save the suffix sort (ARR, arr + S. size (); int Len = S. size (); For (INT I = 0; I <S. size (); I ++) {P [I] = len-Arr [I]. size (); A [Len-Arr [I]. size ()] = I;} A [Len] =-1; C [p [0] = 'a'; For (INT I = 1; I <Len; I ++) {if (a [p [I-1] + 1] <A [p [I] + 1]) c [p [I] = C [p [I-1]; // This is a character we determine to position P [I], // if the current string S1, and the following string S2 (both sorted strings) // compare the size of the strings plus 1. Here I can ignore my explanation else C [p [I] = C [p [I-1] + 1;} string ans; For (INT I = 0; I <Len; I ++) ans + = C [I]; If (ANS <s) Return "exists"; else return "does not exist" ;}}; int main () {suffixarraydiv2 B; string s; CIN> S; cout <B. smallerone (s); Return 0 ;}

 

Topcoder SRM 630div 2

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.