Sorry for disturbing you .
I was trying to solve this SPOJ problem , but failed .
Any suggestion ? Hint ? Tutorial ? Or solution ?
Waiting for positive reply .
My main code
Thanks in advance .
# | User | Rating |
---|---|---|
1 | ecnerwala | 3649 |
2 | Benq | 3581 |
3 | orzdevinwang | 3570 |
4 | Geothermal | 3569 |
4 | cnnfls_csy | 3569 |
6 | tourist | 3565 |
7 | maroonrk | 3531 |
8 | Radewoosh | 3521 |
9 | Um_nik | 3482 |
10 | jiangly | 3468 |
# | User | Contrib. |
---|---|---|
1 | maomao90 | 174 |
2 | awoo | 164 |
3 | adamant | 163 |
4 | TheScrasse | 159 |
5 | nor | 158 |
6 | maroonrk | 156 |
7 | -is-this-fft- | 152 |
8 | SecondThread | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
Sorry for disturbing you .
I was trying to solve this SPOJ problem , but failed .
Any suggestion ? Hint ? Tutorial ? Or solution ?
Waiting for positive reply .
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string s;
cin >> s;
map <char,set<int>>mp;
int tot = s.size();
vector<bool>a(10, 0);
for(int i=0;i<tot;i++)
{
char x = s[i];
mp[x].insert(i);
a[x -'0'] = 1;
}
for(int i=0;i<10;i++)
{
if(a[i]==0)
{
printf("%d",i);
return 0;
}
}
for(int i=10;;i++)
{
string c = to_string(i);
int len = c.size();
char st = c[0];
bool ok = 1;
for(auto it : mp[st])
{
int start = it;
if(start + len > tot) continue;
string cur = s.substr(start,len);
if(cur == c) ok = 0;
}
if(ok==1)
{
printf("%d",i);
return 0;
}
}
}
Thanks in advance .
Name |
---|
The maximum possible answer is around $$$10^5$$$ (or less), just bruteforce over all numbers and check if they occurs should work.
I tried this in the range of 1 to 100000. For all the numbers i converted it to a string using to_strint(val) function. Than i checked all sub string of the length of current value. But TLE.
You can't check the occurrences naively. The complexity will be $$$O(10^5 \cdot n)$$$. You can either precalculate all numbers under $$$10^5$$$ that occurs or build a suffix-automaton on the string.
I don't know about
suffix-automaton
. Can you help with a sample code or tutorial link ?No need to use that. The easiest way to get AC is:
You shouldn't try every possible number. Think what to do with the maximum possible length of the answer.