Friday, May 12, 2017

【UVa】Expanding Fractions

Problem here

Solution

  1. #include <iostream>
  2. #include <memory.h>
  3. #include <string>
  4. using namespace std;
  5. int main(){
  6. int n, m;
  7. int len[1001];
  8. while(cin >> n >> m){
  9. if(n == 0 && m == 0)
  10. break;
  11. memset(len, -1, sizeof(len));
  12. len[n] = 0;
  13. string output = ".";
  14. while(n){
  15. n *= 10;
  16. output += (n/m) + '0';
  17. n %= m;
  18. if(len[n%m] != -1)
  19. break;
  20. len[n] = output.length()-1;
  21. }
  22. for(int i = 0; i < output.length(); i++){
  23. if(i > 0 && (i%50)==0 )
  24. cout << endl;
  25. cout << output[i];
  26. }
  27. cout << endl;
  28. if(n != 0)
  29. cout << "The last " << output.length() - len[n%m] - 1 << " digits repeat forever." << endl;
  30. else
  31. cout << "This expansion terminates." << endl;
  32. //UVa 要再output一空行
  33. //cout << endl
  34. }
  35. return 0;
  36. }

Thursday, May 11, 2017

【UVa】Frogger

Problem here

Solution

自己的石子為點0,目標石子為1
生成MST直到點0點1連通
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory.h>
  4. #include <utility>
  5. #include <string>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <algorithm>
  9. using namespace std;
  10. vector<pair<float, pair<int, int> > > edges;
  11. string spp;
  12. float cal(int x1, int x2, int y1, int y2){
  13. return (float)sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  14. }
  15. bool cmp(pair<float, pair<int, int> > a, pair<float, pair<int, int> > b){
  16. return a.first < b.first;
  17. }
  18. int pre[1000];
  19. int find(int x){
  20. int r = x;
  21. while(pre[r] != r){
  22. r = pre[r];
  23. }
  24. int j = x, y;
  25. while(j != r){
  26. y = pre[j];
  27. pre[j] = r;
  28. j = y;
  29. }
  30. return r;
  31. }
  32. void join(int a, int b){
  33. int fa = find(a);
  34. int fb = find(b);
  35. if(fa != fb){
  36. pre[fa] = fb;
  37. }
  38. }
  39. int main(){
  40. int n;
  41. int count = 1;
  42. while(cin >> n){
  43. if(n==0)
  44. break;
  45. vector<pair<int , int > > point;
  46. for(int i = 0; i < n; i++){
  47. int x, y;
  48. cin >> x >> y;
  49. point.push_back(make_pair(x,y));
  50. for(int j = 0; j < i; j++){
  51. edges.push_back(make_pair(cal(point[i].first, point[j].first, point[i].second, point[j].second), make_pair(i, j)) );
  52. }
  53. }
  54. cout << "Scenario #" << count++ << endl;
  55. sort(edges.begin(), edges.end(), cmp);
  56. for(int i = 0; i < n; i++)
  57. pre[i] = i;
  58. for(int i = 0; i < edges.size(); i++){
  59. if(find(edges[i].second.first)==find(edges[i].second.second))
  60. continue;
  61. join(edges[i].second.first, edges[i].second.second);
  62. if(find(1) == find(0)){
  63. printf("Frog Distance = %.3lf\n", edges[i].first);
  64. break;
  65. }
  66. }
  67. edges.clear();
  68. cout << endl;
  69. getline(cin, spp);
  70. }
  71. return 0;
  72. }

Wednesday, May 10, 2017

【Zerojudge】d908

Problem here

Solution

DFS
  1. #include <iostream>
  2. #include <memory.h>
  3. using namespace std;
  4. int rat[30][30];
  5. bool visit[30];
  6. int sum;
  7. void solve(int here, int cost){
  8. sum = max(sum, cost);
  9. visit[here] = true;
  10. for(int i = 0; i < 26; i++){
  11. if(visit[i] == false && rat[here][i] != 0){
  12. solve(i, cost+rat[here][i]);
  13. visit[i] = false;
  14. }
  15. }
  16. }
  17. int main(){
  18. char start;
  19. while(cin >> start){
  20. int n;cin >> n;
  21. sum = 0;
  22. memset(rat, 0, sizeof(rat));
  23. memset(visit, false, sizeof(visit));
  24. while(n--){
  25. char a, b;int w;
  26. cin >> a >> b >> w;
  27. rat[a-'A'][b-'A'] = max(rat[a-'A'][b-'A'], w);
  28. }
  29. solve(start-'A', sum);
  30. cout << sum << endl;
  31. }
  32. return 0;
  33. }

Monday, May 8, 2017

【UVa】12149 - Feynman

Problem here

Solution

N增加1, 總正方形數會增加N*N


  1. #include <iostream>
  2. #include <memory.h>
  3. using namespace std;
  4. int mark[101];
  5. int main(){
  6. int n;
  7. memset(mark, 0, sizeof(mark));
  8. while(cin >> n){
  9. if(n == 0)
  10. break;
  11. for(int i = 1 ; i <= n; i++)
  12. if(mark[i] == 0)
  13. mark[i] = mark[i-1] + (i*i);
  14. cout << mark[n] << endl;
  15. }
  16. return 0;
  17. }

Wednesday, May 3, 2017

【SPOJ】AIBOHP - Aibohphobia

Problem here

Solution

答案=原字串長度-原字串與其反轉後字串的lcm
一開始用top down寫,TLE orz…. 
之後改成 bottom up 才AC
#include <iostream>
#include <string>
using namespace std;
string input, res;
int lcs[6101][6101];

int main(){

    int kase;
    cin >> kase;
    while(kase--){
        cin >> input;
        res.resize(input.size());
        for(int i = 0; i < input.length(); i++)
            res[i] = input[input.length()-1-i];

        for(int m = 0; m <= input.length(); m++){
            for(int n = 0; n <= res.length(); n++){
                if(m == 0 || n == 0)
                    lcs[m][n] = 0;
                else if(input[m-1] == res[n-1])
                    lcs[m][n] = 1 + lcs[m-1][n-1];
                else
                    lcs[m][n] = max(lcs[m][n-1], lcs[m-1][n]);
            }
        }

        cout << input.length() - lcs[input.length()][res.length()] << endl;
    }

    return 0;
}

Installing xAct (Mac)

Download from http : www.xact.es . Before unpack the tar/tgz file, remove attributes with xattr -c xAct_1.x.x.tgz then place the un...